AngularJS中的函数不调用$WatchGroup
Function in AngularJS not calling to $WatchGroup
我有这个函数,当按下按钮时被激活,它从数组中删除一个对象:
$scope.remove = function (index) {
//$scope.myPeople.splice(index, 1);
$scope.myPeople.splice(index, 1);
console.log($scope.myPeople);
}
在我的$scop.$watchGroup
里,我有这个:
$scope.$watchGroup(['metric.value', 'startDate.value', 'endDate.value', 'myPeople', 'remove'], function () {
angular.forEach($scope.myPeople, function (key) {
peopleArray = peopleArray.concat(key.screenName + ",");
});
peopleArray = peopleArray.slice(0, -1);
peopleArray = peopleArray.concat("}");
});
但是出于某种原因,当我从列表中删除某些内容时,$watchGroup
似乎没有在听它......
这是我在调用中使用的 html 代码,也许它会有所帮助
<a ng-click="remove($index)"
value="{{person}}" type="button"style="color:red">
<i class="fa fa-times"></i></a>
我还在尝试这个没有成功...
$scope.$watchCollection('[myPeople]', function () {
$scope.dataPie.length = 0;
angular.forEach($scope.myPeople, function (value) {
$scope.dataPie.push({
"key": value.name,
"y": value.followerCount
});
});
});
但是当调用此函数时,它不会触发!
$scope.remove = function (index) {
$scope.myPeople.splice(index, 1);
$scope.apply;
}
有什么线索吗?!?
您可以将$watch
与第三个参数true
和返回对象进行监视的自定义函数一起使用,例如
$scope.$watch(
function(scope){
return [scope.myPeople, $scope.$eval('metric.value')];
},
function() {
...
},
true
);
样本
var app = angular.module('plunker', []);
app.controller('ProfileController', function($scope) {
$scope.myPeople = [{
name: '1',
followerCount: 1
},
{
name: '2',
followerCount: 2
},
{
name: '3',
followerCount: 3
},
{
name: '4',
followerCount: 4
}];
$scope.remove = function(index) {
$scope.myPeople.splice(index, 1);
};
$scope.dataPie = [];
$scope.$watch(
function(scope){
return [scope.myPeople, $scope.$eval('metric.value')];
}, function() {
$scope.dataPie.length = 0;
console.log("test");
angular.forEach($scope.myPeople, function(value) {
$scope.dataPie.push({
"key": value.name,
"y": value.followerCount
});
});
},true);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="plunker" class="table-responsive" ng-controller="ProfileController">
<table class="table table-striped">
<thead>
</thead>
<tbody>
<tr ng-repeat="person in myPeople | orderBy:predicate:reverse">
<td><img class="img-rounded" ng-src="{{person.profileImageUrl}}"></td>
<td>{{person.name}}</td>
<td>
<a ng-click="remove($index)" value="{{person}}" type="button" style="color:red">
<i class="fa fa-times"></i>Remover</a>
<br>
</td>
</tr>
</tbody>
</table>
{{dataPie}}
<br><br>
<label>
<input type="radio" ng-model="metric.value" value="11"> valueOne <i class="fa fa-twitter"></i>
</label>
<br/>
<label>
<input type="radio" ng-model="metric.value" value="12"> valueTwo <i class="fa fa-twitter"></i>
</label>
<br/>
<label>
<input type="radio" ng-model="metric.value" value="13"> valueThree <i class="fa fa-twitter"></i>
</label>
</div>
watchGroup
不会对数组内容进行深度扫描
您可能希望使用 $watchCollection
而不是 $watchGroup
。
在此处查看 Angular 文档。
数组既是索引集合,也是对象,这就是为什么可以使用$watchCollection
的原因:
角度代码
$scope.remove = function(index) {
$scope.myPeople.splice(index, 1);
};
$scope.$watchCollection('myPeople', function(newPeople, oldPeople) {
console.log(newPeople);
});
下面是一个 Plunker,显示了代码的运行情况:Plunker
如果替换
$scope.myPeople.splice(index, 1);
跟
$scope.myPeople = $scope.myPeople.slice(0).splice(index, 1);
您不需要额外的$watch。
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- node.js:setInterval()正在跳过调用
- 如何在单击复选框后调用控制器方法
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 使用Google Visualization动态调用构造函数
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 调用函数内部的函数
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- AngularJS中的函数不调用$WatchGroup
- AngularJS:从作用域调用$watchGroup