0
我正在使用BreezeJS和AngularJS,但我很難理解如何讓Promise與$ scope一起工作。每當我嘗試提交表單時,都會顯示驗證錯誤,直到第二次點擊。我意識到我可以調用$ scope。$ apply(),但我讀到它不是最佳做法?這裏是我的代碼:範圍從Promise中更改
app.controller("MainController", ["$scope", "$q", "datacontext", function ($scope, $q, datacontext) {
datacontext.manager.fetchMetadata();
$scope.errors = [];
$scope.addDamp = function() {
var item = datacontext.manager.createEntity("Damp", {
name: $scope.newDamp
});
var tes = datacontext.manager.saveChanges()
.then(function() {
alert("yay");
})
.fail(function (error, a, b, c) {
var arr = [];
error.entitiesWithErrors.map(function (entity) {
entity.entityAspect.getValidationErrors().map(function (validationError) {
arr.push(validationError.errorMessage);
});
});
$scope.errors = arr;
datacontext.manager.rejectChanges();
});
};
}]);
什麼是最好的方式來處理範圍內的變化來自一個承諾內部?
使用'$ scope。$ apply()'只是'不是最佳實踐',不需要它......但這正是它的目的。 http://jimhoskins.com/2012/12/17/angularjs-and-apply.html – thefrontender