剛剛在看angularjs的todoMVC項目,發現它在控制器中自訂了一個過濾器
$scope.$watch('TC.location.path()',function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
});
之後在視圖中
<li ng-repeat="todo in TC.todos | filter:TC.Filters track by $index" ng-class="{completed: todo.completed, editing: todo === TC.editedTodo}">
我的問題主要是第一段程式碼這種寫法不了解
function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
}
這個方法中傳入path後根據path選擇不同的過濾器。
阿神2017-05-15 17:00:57
$scope.$watch('TC.location.path()',function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
});
這裡監聽了路徑的變化,當路徑變化了filter的參數也隨之變化了。其實就是completed和active資料的切換