如何比较自定义指令中 ng-show 中的字符串值?

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

正在尝试使用其中包含 ng-show 语句的指令。基本上它会检查我的 'names' jsonarray:

中 status_p1 属性 的字符串的值
ng-show="name.status_p1==working"

指令定义如下:

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>'
  };
})

我主页中的控制器+名称数组如下所示:

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

  ]
});

最后是主页:

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

目前显示一个叉号,而它应该显示一个 check/tick。我期待条件评估为 TRUE,因为 status_p1 属性 等于 'working'。如何修改此 ng-showstatement 以使字符串比较工作? plunkr link:http://plnkr.co/edit/3VdsbsSHpkNJFVnvkmOW?p=preview

表达式

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

name.status_p1 与当前范围的 working 属性 进行比较,这在您的案例中未定义。您需要的是将它与文字字符串 'working'.

进行比较
ng-show="name.status_p1=='working'";

已修改Plunkr

就我而言,我有这个:

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

不得不将其更改为:

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

我把属性字符串用单引号括起来,要比较的字符串用双引号括起来。