gpt4 book ai didi

javascript - Angular js : change which variables to watch

转载 作者:行者123 更新时间:2023-12-01 02:13:26 24 4
gpt4 key购买 nike

我想更改要观察的变量。

JS:

var app = angular.module('myApp', []);

app.controller('MainController',
function($scope) {

$scope.current = null;
$scope.select = function(item) {
$scope.current = item;
};

var watch = ['current'];
$scope.$watchGroup(watch, function() { // This does not work
if ($scope.current !== null) {
watch = ['current'];
Array.prototype.push.apply(watch, $scope.current.watch);
$scope.current.callBack();
}
});

$scope.list = [
{
name: 'test-A',
watch: ['a'],
callBack: function() {
$scope.value = 2 * $scope.a;
}
},
{
name: 'test-B',
watch: ['b'],
callBack: function() {
$scope.value = 3 * $scope.b;
}
}
];

});

HTML:

<!DOCTYPE html>
<html ng-app="myApp">

<head>
<link data-require="bootstrap@3.3.7" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script data-require="jquery" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.slim.min.js"></script>
<script data-require="bootstrap@3.3.7" data-semver="3.3.7" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script data-require="angular.js@1.6.6" data-semver="1.6.6" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body ng-controller="MainController">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
{{current != null ? current.name : 'DropDown'}}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li ng-repeat="item in list">
<a href="javascript:void(0)" ng-click="select(item)">{{item.name}}</a>
</li>
</ul>
</div>
When "test-A" is selected, result is 2 times of A.
<br> When "test-B" is selected, result is 3 times of B.

<br>
<br>
<br>

<form class="form-inline">
<div class="form-group">
<label>A</label>
<input type="text" class="form-control" ng-model="a">
</div>
<div class="form-group">
<label>B</label>
<input type="text" class="form-control" ng-model="b">
</div>
</form>

<br> result : {{value}}
<br>
</body>

</html>

This是 Plunker 的例子。

选择test-A时,结果应为A的2倍,应为B的3倍 code> 当选择 test-B 时。

我想更新要监视的变量。但是 This does not work 行中显示的方式不起作用。此 $watchGroup 仅在 current 更改时触发。我想在选择 test-A 的情况下,在 currenta 发生更改时触发此 $watchGroup,当选择 test-B 时,当 currentb 发生变化时。

我知道 $scope.$watchGroup(['a', 'b', 'current'], ... 有效。但我不喜欢触发这个 $watchGroup 当选择 test-A 并更改 b 时,反之亦然。

如何更新要监视的变量?

最佳答案

找到以下代码作为您的答案,看看它是否对您有帮助。

脚本

var app = angular.module('myApp', []);

app.controller('MainController',
function($scope) {

$scope.current = null;
$scope.select = function(item) {
$scope.current = item;
};

var watch = ['current'];
$scope.$watchGroup(watch, function() {
if ($scope.current !== null) {
watch = ['current'];
Array.prototype.push.apply(watch, $scope.current.watch);
$scope.current.callBack();
}
});

$scope.list = [
{
name: 'test-A',
watch: [' is two times of a'],
callBack: function() {
$scope.value = (2 * $scope.a) + ($scope.current.watch);
}
},
{
name: 'test-B',
watch: [' is three times of b'],
callBack: function() {
$scope.value = (3 * $scope.b) + ($scope.current.watch);
}
}
];

});

我刚刚将 ($scope.current.watch) 与您的结果连接起来

看看它是否 fork 到你的 plnkar。

https://plnkr.co/edit/UGPe4lgJPydEeAXOc0xR?p=preview

关于javascript - Angular js : change which variables to watch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49605255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com