将对象添加到数组不会刷新ng-repeat

时间:2014-02-19 18:32:07

标签: javascript angularjs

有景点列表,当用户选择这些项目时,将转到所选列表,

app.controller('ItineraryNewController', function($scope) {
$scope.city = "";
$scope.attractions =[];
$scope.places = [] ;
$scope.day = 1;
$scope.getAttractions=function(){

        $scope.attractions.push({
        name: "LA",
            description: "test la",
              address: "3423 some stree",
              website: "test.com"
          },,
          {
        name: "SFO",
            description: "test SFO",
              address: "3423 some stree",
              website: "testsfo.com"
          });

}
$scope.addPlace = function(place,index){
  console.log(index);
    $scope.places.push($scope.attractions[index]);
}

这些地方没有更新,我认为索引值是未定义的。

该示例发布在plunker Plunker

1 个答案:

答案 0 :(得分:1)

您没有从模板中向函数addPlace传递正确数量的参数。

从您的功能定义addPlace(place, index)

<button ng-click="addPlace($index)">Add</button>

应该是

<button ng-click="addPlace(place, $index)">Add</button>