如何在ng -repeat中将新内容附加到angulularJS模板中而不破坏以前的内容

时间:2015-12-24 12:56:06

标签: angularjs

如何在ng -repeat中将新内容附加到angulularJS模板中,而不会破坏以前的内容 1.我想在每次保存点击中显示myvar数据                              

<script>
    var myApp = angular.module('myApp', []);

    myApp.controller('MyController', ['$scope', '$http', function ($scope,        $http, $filter, $rootScope) {

        $scope.view= function () {
             //some ajax calll

                  $scope.myvar = data;
        };
    }]);

</script>
</head>
<body ng-app="myApp" ng-controller="MyController">
<ul>
    <li class="Status" ng-repeat="item in myvar">
        {{item.var}}
    </li>
</ul>
<button ng-click="view()">View More</button>

    我请建议我

2 个答案:

答案 0 :(得分:0)

根据ng-repead documentation(查看跟踪和重复部分)“跟踪$ index”是您需要的

<li class="Status" ng-repeat="item in myvar track by $index">
  {{item.var}}
</li>

您也可以使用“跟踪yuor_primary_key”。

答案 1 :(得分:0)

你需要每次在阵列中推送新元素
请检查此代码:http://codepen.io/anon/pen/bEwBwQ

控制器

angular.module('DemoApp', ['ui.router', 'ngAnimate'])
  .controller('DemoController', function($scope, $window, $state) {
    $scope.myvar = [{"var" : 1}];
    $scope.save = function() {
      //some ajax calll
      var v = {"var" : 1};
      $scope.myvar.push(v);
    };

  });

HTML

<ul>
  <li class="Status" ng-repeat="item in myvar">
    {{item.var}}
  </li>
</ul>
<button ng-click="save()">save</button>