Angular-JS ng-repeat with asynchron data

时间:2015-11-06 10:16:27

标签: javascript json angularjs asynchronous angularjs-ng-repeat

我有一个包含很多物品的商店。所有这些项目都属于一个类别。所以我在我的商店里有一个功能来获取所有现有的类别。

我的代码: http://plnkr.co/edit/rB4NMezhexEX4aFqveUO

但是在加载商店后,$ scope将被更新,但不会更新ng-repeat。什么是正确的角度方式呢?

此致 Bytecounter

1 个答案:

答案 0 :(得分:0)

所以我扰乱了你的代码,我认为这就是你需要的东西:

JS

function ClickToEditCtrl($scope) {
  $scope.products = [{name: 'Mercedes', type: "car"},
                     {name: 'Ford', type: "car"},
                     {name: 'BMW', type: "car"}];

  $scope.submitProduct = function () {
    $scope.products.push({name: 'Fiat', type: 'car'});
  };

}

HTML:

<div ng-app>
  <div ng-controller="ClickToEditCtrl">
    <div data-ng-repeat="product in products">
        {{product.name}}
      </div>
      <input type="button" ng-click="submitProduct()"  value="Click me">
  </div>
</div>

小提琴: http://jsfiddle.net/5DMjt/3459/

所以ng-repeat列出了arrey中的所有对象,发生了一些事情(在我的情况下单击动作)并且该数组发生了变化,你只需要将其推入内部。

希望有所帮助