Javascript 使用ng repeat处理多个ng show
我有一个使用ng repeat的项目列表。每个项目有两个按钮“跟随”和“取消跟随”。 我需要根据已经跟随的项目(数组列表包含跟随的项目)一次显示这两个项目中的任何一个 在我的html中:Javascript 使用ng repeat处理多个ng show,javascript,angularjs,angularjs-ng-repeat,ng-repeat,ng-show,Javascript,Angularjs,Angularjs Ng Repeat,Ng Repeat,Ng Show,我有一个使用ng repeat的项目列表。每个项目有两个按钮“跟随”和“取消跟随”。 我需要根据已经跟随的项目(数组列表包含跟随的项目)一次显示这两个项目中的任何一个 在我的html中: <ion-list> <ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="popShow in popShows" type="item-text-wrap" href="#/tab/po
<ion-list>
<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="popShow in popShows" type="item-text-wrap" href="#/tab/popular/{{popShow.id}}" back-img={{popShow.backdrop_path}}>
<h2>{{popShow.name}}</h2>
<p>{{popShow.overview}}</p>
<i class="icon ion-chevron-right icon-accessory"></i>
<ion-option-button class="button-assertive" ng-click="follow(popShow)" id="followB" ng-show="showFollowButton">
Follow
</ion-option-button>
<ion-option-button class="button-assertive" ng-click="unFollow(popShow)" id="unFollowB" ng-show="showUnFollowButton">
Following
</ion-option-button>
</ion-item>
</ion-list>
{{popShow.name}
{{popShow.overview}
跟随
下列的
在我的控制器中,我需要更改显示的特定项目的值。
我正试着这样做
controller.js:
.controller('PopularShowsCtrl', function($scope, PopularShows){
var followArray = PopularShows.getFollowArray();
PopularShows.all().success(function(data){
$scope.popShows = data.results;
for (var i = 0; i < $scope.popShows.length; i++) {
var currentItem = $scope.popShows[i];
if ($.inArray(currentItem.id, followArray) > -1)
{
console.log("Following show: " + currentItem.name);
currentItem.showFollowButton = false;
currentItem.showUnFollowButton = true;
}else{
currentItem.showUnFollowButton = false;
currentItem.showFollowButton = true;
}
}
...
.controller('PopularShowsCtrl',function($scope,PopularShows){
var followArray=PopularShows.getFollowArray();
PopularShows.all().success(函数(数据){
$scope.popShows=data.results;
对于(变量i=0;i<$scope.popShows.length;i++){
var currentItem=$scope.popShows[i];
if($.inArray(currentItem.id,followArray)>-1)
{
log(“以下显示:”+currentItem.name);
currentItem.showFollowButton=false;
currentItem.showUnFollowButton=true;
}否则{
currentItem.showUnFollowButton=false;
currentItem.showFollowButton=true;
}
}
...
currentItem.show(Un)FollowButton=true/false不起作用。
我该怎么做呢
注意:我尝试了nf show中的按钮功能,该功能将在页面加载时运行并获取真/假值,但当用户按下“跟随”或“取消跟随”按钮时,出现了如何再次更改该按钮的问题
谢谢!您已经在for循环中为每个元素绑定了
showUnFollowButton
和showFollowButton
属性。但是在Html上使用它们时,您没有正确使用它
您需要将ng show
更改为下文,因为您已经将这些属性绑定到for循环中的每个元素
ng-show="popShow.showUnFollowButton"
ng-show="popShow.showFollowButton"
谢谢Pankaj。这解决了问题,尽管我不明白为什么。@user2984127请看更新后的答案。.基本上您的代码是正确的。.但没有在html上使用该属性绑定。@user2984127很高兴知道这一点。.谢谢:)