我想了解$ watch如何通過示例工作,但不工作。在這個例子中,我正在觀看一個名爲收藏夾的數組,該數組位於$範圍內。如果收藏夾數組發生變化,我希望在控制檯中寫入新值。不知道這是否是使用$ watch的正確方法。
控制器代碼
var mods = angular.module("listApp",[])
mods.controller("prodCtlr", function($scope){
$scope.favorites = ["a", "b", "c", "d"]
$scope.delete = function(index){
$scope.favorites.splice(index,1)
}
$scope.$watch(function(){
return $scope.favorites;
}, function(newVal, oldVal){
console.log(newVal);
})
}
);
HTML
<table class="table table-striped">
<tr ng-repeat="fav in favorites">
<td>{{fav}}</td><td><input type="button" class="btn btn-primary" value="Delete!" ng-click="delete($index)" ng-model="fav"></input></td>
</tr>
</table>
輝煌,適合我!謝謝。 – tintin