如果value大于1,则Angularjs ng-disabled被禁用

时间:2021-08-06 19:42:06

i have some values coming from json. if my values are 0, the click is working fine. but if the values from json are greater than 0, the button is getting disabled and not working initially. i am trying to do like and dislike options using this code. so that user can select like button" one time or "dislike button" one time.any solution for this?

我有一些来自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;
                        };

1 个解决方案

#1


2  

You already have all the controller functionality you need.

您已拥有所需的所有控制器功能。

Just change your markup to:

只需将您的标记更改为:

<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>

Note that I got rid of the ng-inits that didn't do anything, and just disabled each button if it had already been clicked.

请注意,我删除了没有执行任何操作的ng-inits,并且如果已经单击了按钮,则禁用每个按钮。

#1


2  

You already have all the controller functionality you need.

您已拥有所需的所有控制器功能。

Just change your markup to:

只需将您的标记更改为:

<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>

Note that I got rid of the ng-inits that didn't do anything, and just disabled each button if it had already been clicked.

请注意,我删除了没有执行任何操作的ng-inits,并且如果已经单击了按钮,则禁用每个按钮。