$ 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