[英]AngularJS Resolving a Promise
我正在學習如何使用示例中的 resolve,並將其應用於我的Todo腳本。
然后我意識到一個問題,該示例僅向我展示了當我第一次訪問此路線時如何解析GET調用以獲取Todo列表。
但是,在相同路徑的同一頁面中,我有一個添加按鈕來POST新的待辦事項項,還有一個清除按鈕來刪除已完成的項目。
查看我的$scope.addTodo = function() {
和$scope.clearCompleted = function () {
我希望在操作后再次解析我的TodoList。 我怎樣才能做到這一點?
這是我的代碼。 在我的代碼中,初始resolve: { todos: TodosListResl }
正在工作,它命中TodosListResl
函數並產生promise。 但是,當我想再次解析待辦事項列表時,我不知道如何處理addTodo
和clearComplete
。
main.js
var todoApp = angular.module('TodoApp', ['ngResource', 'ui']);
todoApp.value('restTodo', 'api/1/todo/:id');
todoApp.config(function ($locationProvider, $routeProvider) {
$routeProvider.when("/", { templateUrl: "Templates/_TodosList.html",
controller: TodosListCtrl, resolve: { todos: TodosListResl } });
$routeProvider.otherwise({ redirectTo: '/' });
});
//copied from example, works great
function TodoCtrl($scope, $rootScope, $location) {
$scope.alertMessage = "Welcome";
$scope.alertClass = "alert-info hide";
$rootScope.$on("$routeChangeStart", function (event, next, current) {
$scope.alertMessage = "Loading...";
$scope.alertClass = "progress-striped active progress-warning alert-info";
});
$rootScope.$on("$routeChangeSuccess", function (event, current, previous) {
$scope.alertMessage = "OK";
$scope.alertClass = "progress-success alert-success hide";
$scope.newLocation = $location.path();
});
$rootScope.$on("$routeChangeError",
function (event, current, previous, rejection) {
alert("ROUTE CHANGE ERROR: " + rejection);
$scope.alertMessage = "Failed";
$scope.alertClass = "progress-danger alert-error";
});
}
//also copied from example, works great.
function TodosListResl($q, $route, $timeout, $resource, restTodo) {
var deferred = $q.defer();
var successCb = function(resp) {
if(resp.responseStatus.errorCode) {
deferred.reject(resp.responseStatus.message);
} else {
deferred.resolve(resp);
}
};
$resource(restTodo).get({}, successCb);
return deferred.promise;
}
//now, problem is here in addTodo and clearCompleted functions,
//how do I call resolve to refresh my Todo List again?
function TodosListCtrl($scope, $resource, restTodo, todos) {
$scope.src = $resource(restTodo);
$scope.todos = todos;
$scope.totalTodos = ($scope.todos.result) ? $scope.todos.result.length : 0;
$scope.addTodo = function() {
$scope.src.save({ order: $scope.neworder,
content: $scope.newcontent,
done: false });
//successful callback, but how do I 'resolve' it?
};
$scope.clearCompleted = function () {
var arr = [];
_.each($scope.todos.result, function(todo) {
if(todo.done) arr.push(todo.id);
});
if (arr.length > 0) $scope.src.delete({ ids: arr });
//successful callback, but how do I 'resolve' it?
};
}
我想你錯過了resolve
。 resolve
是“ 在數據加載之前延遲路由更改 。在您的情況下,您已經在路由中,並且您希望保持該路由。但是,您希望在成功回調時更新todos
變量。”在這種情況下,您不想使用resolve
。而只需執行需要完成的操作。例如
$scope.addTodo = function() {
$scope.src.save({ order: $scope.neworder,
content: $scope.newcontent,
done: false }, function () {
todos.push({ order: $scope.neworder,
content: $scope.newcontent,
done: false });
});
//successful callback, but how do I 'resolve' it?
};
作為一個側面點,我注意到你正在使用_
最有可能來自Underscore庫。 你不需要使用另一個庫,因為Angular已經有了$angular.forEach()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.