$ watch()没有更新$ scope

从我的草坪上离开

我有以下控制器,当我调用$scope.remove()时,它向usercart发出请求,后者向api发出请求。api返回json对象,该对象具有包含购物车商品数组的对象。

页面上的html使用ng-repeat来遍历项目,但是由于某些原因该页面未更新,因此我无法弄清原因。

// Main controller
app.controller('Checkout', function($scope, usercart){

    $scope.cart = [];

    $scope.$watch(function(){
        return usercart.cart;
    }, function(newVal, oldVal){
        if(newVal !== oldVal){
            $scope.cart = newVal;
        }
    }, true);

    $scope.remove = function(domain){
        usercart.remove(domain);
    };

});

该服务向api发出请求并保存购物车数据。

// User cart service
app.service('usercart', function(cart){
    this.remove = function(domain){
        // cart is an api service to make http requests
        cart.removeDomain(domain).success(function(data){
            this.cart = data.cart;
        });
    };
});

这是一个json响应示例:

{
    "message":"Success",
    "cart":[{
        "domain":"asdfsadfsadf.org",
        "years":2,
        "amount":9
    },{
        "domain":"asdsmembers.cc",
        "years":2,
        "amount":24.95
    },{
        "domain":"asdsmembers.tv",
        "years":2,
        "amount":39.95
    }]
}

这是html:

<tr ng-repeat="i in cart">
    <td data-th="Product">
        {{i.domain}}
    </td>
    <td data-th="Price">${{i.amount|number:2}}</td>
    <td data-th="Quantity">
        <select ng-model="i.years" ng-options="y.value as y.name for y in selYears" ng-disable="isInCart(i.domain)" ng-class="{disabled: isInCart(i.domain)}" ng-change="update(i.domain, 'years', i.years)"></select>
    </td>
    <td class="actions" data-th="" align="center">
        <button class="btn btn-default btn-sm" style="background: #333;" ng-click="remove(i.domain)"><span class="glyphicon glyphicon-remove-circle" aria-hidden="true" style="color:#fff;"></span></button>
    </td>
    <td data-th="Subtotal" class="text-center">${{i.years * i.amount|number:2}}</td>
</tr>

同样,在页面加载时,表也可以正常显示。就在我运行删除功能时。

达扬·波格丹(Darjan Bogdan)

我没有尝试过,但我相信这是问题所在

cart.removeDomain(domain).success(function(data){
        this.cart = data.cart;
    });

由于this是指向回调函数调用方的指针,因此您将cartcartapi服务创建属性为了规避此问题,您应该创建一个名为(按惯例)的变量selfthis(在usercart服务开始时分配给它

var self = this;

之后,将您的代码更改为:

cart.removeDomain(domain).success(function(data){
        self.cart = data.cart;
    });

为了更好地理解您可以阅读这篇文章

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

$ scope。$ watch似乎没有观看工厂变量

没有$ scope.digest,$ watch不会触发

在function($ scope)旁边没有['$ scope'

属性更新时不会触发$ scope。$ watch

我的ng模型没有更新我的$ scope值?

如何在Angular 1.5组件中等待绑定(没有$ scope。$ watch)

在没有$ scope或$ watch的情况下跨Angular控制器访问函数

没有$ scope。$ watch的服务和控制器之间的双向绑定。这是不好的做法吗?

角$ scope在更新,导航和$ scope之后没有绑定到视图。$ apply()

$ scope或$ scope。$ watch中的作用域?

$ scope。$ watch和$ watch之间有什么区别?

在Angular JS中,有没有办法在不使用scope.watch的情况下监视DOM的更改?

从指令添加值时,数组上的$ scope。$ watch不更新

angularjs指令未使用scope。$ watch进行更新

没有$ scope的控制器

AngularJS使用$ apply而没有$ scope

AngularJS $scope 没有显示价值

AngularJS $ scope未更新

使用Firebase更新$ scope

$scope.$watch 如何工作

AngularJS $ scope。$ watch对象的属性

在$ scope。$ watch上实施延迟

angularJS scope。$ watch整个对象

角$ scope。$ watch newVal!== oldVal

为什么$ scope变量里面的指令没有得到更新?

没有$ scope,如何在Promise.all()中更新AngularJS(1.7.x)中的DOM?

bindToController如何摆脱所有$ scope($ scope。$ on)

是否有类似于$ scope。$ watch的NSMutableArray之类的东西

角度$ scope。$ watch具有可返回承诺的函数