[英]AngularJs promise not updating shared Shopping Cart
我不确定为什么该客户数据不会更新shoppingCart.html模板,该函数是工厂返回的共享模型类的一部分。
$ http被注入,但是模型类无法在promise结果中进行自我更新。 因此,模板终于更新了。 最好的方法是什么?
http://plnkr.co/edit/ro6FTa5AbhnzrMsnWIyi?p=preview
shoppingCart.prototype.fillCustomerInformation = function()
{
var customerDeferred = this.$q.defer();
var customerApi = customerDeferred.promise;
this.$http.get('/customer.json').then(function (result) {
customerDeferred.resolve(result.data);
console.log(result.data);
this.customer.firstName = result.data.firstName;
this.saveItems()
});
}
问题是, this
涉及成功的ajax处理程序。 你可以这样做:
shoppingCart.prototype.fillCustomerInformation = function()
{
var customerDeferred = this.$q.defer();
var customerApi = customerDeferred.promise;
var that = this;
this.$http.get('/customer.json').then(function (result) {
customerDeferred.resolve(result.data);
console.log(result.data);
that.customer.firstName = result.data.firstName;
that.saveItems()
});
}
首先,您在result.data.firtName
firstName
拼写错误,而不是result.data.firstName
其次,有很多方法可以将服务成员绑定到作用域变量,但是我发现最好的方法是像
$scope.$watch(
function () {
return DataService.cart;
},
function (newVal, oldVal) {
// do your stuff to manage updates here
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.