$ watch我试图了解问题出在我使用的$ scope。$ watch,但我无法弄清楚为什么函数中定义的手表没有被解雇。我有这个多选在角材料:
<md-select multiple ng-model="selectedItems" placeholder="Please select">
<md-option ng-repeat="item in specialities" ng-value="item" ng-class="{'selected': item.selected}">
<ng-md-icon class="checkboxfull" icon="check_box" size="18"></ng-md-icon>
<ng-md-icon class="checkboxoutline" icon="check_box_outline_blank" size="18"></ng-md-icon>
{{item.name}}
</md-option>
</md-select>
,并在控制器我打电话$手表这一观点,这应该是我soppose selectedItems:
$scope.$watch('selectedItems', function(){
console.log("$scope.selectedItems: ",$scope.selectedItems);
$scope.filter.idList.length = 0;
angular.forEach($scope.selectedItems, function (item) {
$scope.filter.idList.push(item.id);
});
console.log("$scope.filter.idList: ",$scope.filter.idList);
});
但永远达不到日志并且$ scope.filter.idList永远不会被填充。我看到了使用此解决方案的其他stackoverflow示例,例如AngularJS: $watch for a select input。 谢谢你指点我正确的方向!
从内存,如果它是你在看一个数组,你需要做一些额外的。例如,观察长度变化或做一个深入的观察,像http://stackoverflow.com/questions/14712089/how-to-deep-watch-an-array-in-angularjs – Arkiliknam