[英]AngularJs promise not updating shared Shopping Cart
I'm not sure why this customer data doesn't update the shoppingCart.html template, this function is part of a shared model class returned by a factory. 我不确定为什么该客户数据不会更新shoppingCart.html模板,该函数是工厂返回的共享模型类的一部分。
the $http is injected but the model class can't manage to update itself inside the promise result. $ http被注入,但是模型类无法在promise结果中进行自我更新。 So the template is finally updated. 因此,模板终于更新了。 What would be the best way to do that? 最好的方法是什么?
http://plnkr.co/edit/ro6FTa5AbhnzrMsnWIyi?p=preview 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()
});
}
The problem is, that this
refers to the success ajax handler. 问题是, this
涉及成功的ajax处理程序。 You can do this: 你可以这样做:
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()
});
}
Well for a start you have firstName
spelt wrong in result.data.firtName
instead of result.data.firstName
首先,您在result.data.firtName
firstName
拼写错误,而不是result.data.firstName
Secondly there are many ways to bind service members to scope variables but what i found was the best way of doing that is to do stuff like 其次,有很多方法可以将服务成员绑定到作用域变量,但是我发现最好的方法是像
$scope.$watch(
function () {
return DataService.cart;
},
function (newVal, oldVal) {
// do your stuff to manage updates here
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.