how to compare a stringvalue in ng-show inside a customdirective?

时光毁灭记忆、已成空白 提交于 2019-11-30 16:28:09

问题


Trying to use a directive with an ng-show statement in it. Basically it checks against the value of a string which is the status_p1 property in my 'names' jsonarray:

ng-show="name.status_p1==working"

The directive is defined as this:

app.directive('radioButton',function(){
  return {
    restrict: 'E',

    replace: 'true',

    template: '<table border="2px">' +
    '<tr><td>{{name.name}}</td><td>Working</td><td><img src="http://www.iconshock.com/img_jpg/REALVISTA/general/jpg/256/cross_icon.jpg" alt="img1" id="imgworking" ng-show="name.status_p1!=working"><img src="http://png-1.findicons.com/files/icons/2198/dark_glass/128/camera_test.png" alt="img2" ng-show="name.status_p1==working"></td></tr>' +
    '</table>'
  };
})

The controller+ namesarray in my main page looks like this:

 app.controller('MainCtrl', function($scope) {
 $scope.names = [
    {
      name: 'couple 1',
      status_p1: 'working',
      status_p2: 'retired'
    }

  ]
});

And finally the main page:

<body ng-controller="MainCtrl">
    <div ng-repeat="name in names">
      <radio-button></radio-button>
    </div>
</body>

Currently is displays a cross where it should be displaying a check/tick. I was expecting the condition to evaluate to TRUE because the status_p1 property equals 'working'. How can I modify this ng-showstatement to make the string comparison working? plunkr link:http://plnkr.co/edit/3VdsbsSHpkNJFVnvkmOW?p=preview


回答1:


The expression

ng-show="name.status_p1==working"

compares name.status_p1 with a working property on the current scope, which is not defined in your case. What you need is to compare it with the literal string 'working'.

ng-show="name.status_p1=='working'";

Modified Plunkr




回答2:


In my case, I had this:

ng-show ="authenticated == {{it.logged_in_view}} || {{it.logged_in_view == 'neutral'}}"

and had to change it to this:

ng-show ='authenticated == {{it.logged_in_view}} || {{it.logged_in_view == "neutral"}}'

I enclosed the attribute string in single quotes and the string to be compared in double quotes.



来源:https://stackoverflow.com/questions/28605145/how-to-compare-a-stringvalue-in-ng-show-inside-a-customdirective

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!