我有一個刪除按鈕指令看起來像待辦事項列表,範圍變化不會反映
Howerver,當我的垃圾桶按鈕點擊,視圖沒有得到新的待辦事項刷新。我用scope.$apply()
迫使消化週期,仍不起作用
HTML代碼,
<div ng-controller="todoCtrl">
<ul>
<li ng-repeat="todo in todos">
<i class="fa fa-trash-o fa-lg" sk-del="{{todo}}" todos="todos"></i>{{todo}}
</li>
</ul>
</div>
Javascript代碼,
var app = angular.module('app', []);
app.controller('todoCtrl', function ($scope, $element) {
$scope.todos = ["eat", "pray", "live"]
})
app.directive("skDel", function(){
return {
scope:{
todos: '='
},
link: function(scope, elem, attr){
elem.bind("click", function(){
scope.todos = scope.todos.filter(function(el){
console.log("the return value for todo - " + el + " is " + (el != attr.skDel))
return el != attr.skDel
})
console.log("new set of todos ----" + JSON.stringify(scope.todos))
scope.$apply()
})
}
}
})
控制檯日誌,
the return value for todo - eat is false
the return value for todo - pray is true
the return value for todo - live is true
new set of todos ---- ["pray","live"]
http://jsfiddle.net/arunpjohny/64gmo7jb/ –