【发布时间】:2015-02-04 19:54:20
【问题描述】:
我有一个非常大的列表,它是一个名为 leagues 的数组,我需要允许用户获取该数组(列表)上的元素,然后通过单击按钮将它们选择为收藏夹
$scope.favoriteLeagues = [];
$scope.favoriteLeague = function(league) {
$scope.favoriteLeagues.push(league);
}
所以我想知道我做错了什么?该功能有时允许我将一个添加为收藏,但是一旦我单击第二个,我会收到一条未定义的消息,而且绑定不起作用,我无法看到打印的 {{favoriteLeagues.name}}。
已按要求更新
<div>
<strong>Favorites</strong>
{{favoriteLeagues.name}}
</div>
<ion-option-button class="button-light icon ion-star"
on-tap="favoriteLeague(league)">
</ion-option-button>
<div ng-repeat="sport in sportsFilter = (sports | filter:query)">
<strong>{{sport.name}}</strong>
</div>
<ion-item ng-repeat="league in sport.leagues">
<div>{{league.name}}</div>
</ion-item>
</ion-list>
这里是控制器:
.controller('SportsController', function($scope, $state,
AuthFactory, SportsFactory) {
$scope.favoriteLeagues = [];
$scope.sports = [];
AuthFactory.getCustomer().then(function(customer) {
$scope.customer = customer;
SportsFactory.getSportsWithLeagues(customer).then(function(sports) {
if (sports.length) {
$scope.sports = sports;
}
$scope.isSportShown = function(sport) {
return $scope.shownSport === sport;
};
$scope.favoriteLeague = function(league) {
$scope.favoriteLeagues.push(league);
}
};
});
【问题讨论】:
-
您的数组和函数使用相同的名称,您只能有一个,您需要重命名其中一个
-
@NietzscheProgrammer -- 因为
favoriteLeagues是一个数组!你需要指定一个索引! -
我们能有完整的 HTML 上下文吗?就像这些东西在 ng-repeat 中一样吗?您如何显示各种对象?
-
另外你定义了 $scope.favouriteLeague 但正试图进入 $scope.favouriteLeagues - 注意额外的 's'
-
我们可以看看你的控制器代码吗?
标签: javascript arrays angularjs