简体   繁体   English

AngularJS ng-click创建子范围,但在随后的ng-click之后推送到主范围

[英]AngularJS ng-click creates child scope but pushes to main scope after subsequent ng-click

When I use my first ng-click it creates a child scope but won't push it to parent scope. 当我第一次使用ng-click时,它会创建一个子范围,但不会将其推送到父范围。
Later when I use ng-click again (a very simple seemingly unrelated function) it then seems to jump into main scope and carry out my previous request. 稍后,当我再次使用ng-click(一个非常简单的看似无关的功能)时,它似乎跳入了主要范围并执行了我之前的请求。

Any ideas? 有任何想法吗? I'm stumped but assume its to do with listeners or watch. 我很困惑,但认为与听众或观看有关。

FIRST ng-click: 第一次ng-click:

ng-click="cart.addItem(product.sku, product.name, product.price, 1)

 shoppingCart.prototype.addItem = function (sku, name, price, quantity) {

        this.item = [];  
        this.item = {
          sku : sku,
          name :name,
          price : price,
          quantity:quantity

        };
        this.items.push(this.item);


    };

SECOND ng-click: 第二ng-单击:

ng-click="upload = true"

Thanks, 谢谢,

Will

You should probably go through Angular tutorial, you seem a little bit confused: https://docs.angularjs.org/tutorial/step_00 您可能应该阅读Angular教程,您似乎有些困惑: https : //docs.angularjs.org/tutorial/step_00

Anyway, you have several problems with your code and some obscure parts 无论如何,您的代码有一些问题,一些晦涩的部分

I don't know what this shoppingCart is, but as a general rule you can surely substitute this 我不知道这个shoppingCart是什么,但是作为一般规则,您当然可以用它代替

 shoppingCart.prototype.addItem = function (...)

with

 shoppingCart.addItem = function (...)

Then, in your code you have this snippet: 然后,在您的代码中有以下代码段:

    // everytime you run this function, you substitute whatever 
    // item was with an empty array
    this.item = [];  

    // then you substitute the empty array with an object (?)
    this.item = {
      sku : sku,
      name :name,
      price : price,
      quantity:quantity

    };

    // then you try to push your object in a non-existent array, 
    // or in the array you just overwrote (if you meant 'item' instead of 'items')...
    this.items.push(this.item); 

This code cannot work, so if you see some changes in the $scope it should be for some other reason. 该代码无法正常工作,因此,如果您在$scope看到某些更改,则应该是出于其他原因。

Maybe you need create a directive, bind a click parameter to put it on DIV element. 也许您需要创建一个指令,将click参数绑定到DIV元素上。 This user did a similar code (by Jerrad) . 该用户执行了类似的代码(由Jerrad编写)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM