Javascript 向数组中添加对象不会刷新或重复
有一个景点列表,当用户选择这些项目时,将转到一个选定的列表Javascript 向数组中添加对象不会刷新或重复,javascript,angularjs,Javascript,Angularjs,有一个景点列表,当用户选择这些项目时,将转到一个选定的列表 app.controller('ItineraryNewController', function($scope) { $scope.city = ""; $scope.attractions =[]; $scope.places = [] ; $scope.day = 1; $scope.getAttractions=function(){ $scope.attractions.push({ name
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中,您没有从模板向函数
addPlace
传递正确数量的参数
从函数定义addPlace(place,index)
这
添加
应该是
添加
谢谢,我忘了保存,因为plunker有自动保存功能。这个链接现在是正确的,因为我删除了attractionService。你知道为什么它不起作用吗?请更新你的问题,因为我不太确定你想要实现什么。如果你现在查看链接,按“获取列表”,然后单击“添加”,我看不到列表正在更新
<button ng-click="addPlace($index)">Add</button>
<button ng-click="addPlace(place, $index)">Add</button>