我有一些来自json的值。如果我的值为0,则单击工作正常。但是如果json中的值大于0,则该按钮将被禁用并且最初不起作用。我试图使用此代码做喜欢和不喜欢的选项。这样用户就可以选择按钮"一次或不喜欢按钮"一次。对此有什么解决方案?
//html
<button class="label" ng-disabled="tvshow.episode.ratings.loved" ng-init="tvshow.episode.ratings.loved" ng-click="likeme(tvshow.episode)" >
<i class="icon-thumbs-up"></i>
{{tvshow.episode.ratings.loved}}</button>
<button class="label" ng-disabled="tvshow.episode.ratings.hated" ng-init="tvshow.episode.ratings.hated" ng-click="dislike(tvshow.episode)" >
<i class="icon-thumbs-down"></i>
{{tvshow.episode.ratings.hated}}</button>
//added click event for like and dislike buttons in js
$scope.likeme = function(episod){
episod.ratings.loved = parseInt(episod.ratings.loved) + 1;
if (parseInt(episod.ratings.hated) > 0)
episod.ratings.hated = parseInt(episod.ratings.hated) - 1;
episod.liked = true;
episod.disliked = false;
};
答案 0 :(得分:2)
您已拥有所需的所有控制器功能。
只需将您的标记更改为:
<button class="label" ng-disabled="tvshow.episode.liked" ng-click="likeme(tvshow.episode)">
<i class="icon-thumbs-up"></i>
{{tvshow.episode.ratings.loved}}
</button>
<button class="label" ng-disabled="tvshow.episode.disliked" ng-click="dislike(tvshow.episode)">
<i class="icon-thumbs-down"></i>
{{tvshow.episode.ratings.hated}}
</button>
请注意,我删除了没有做任何事情的ng-init
,如果已经点击了每个按钮,则禁用它。