如何在customdirective内的ng-show中比较字符串值? [英] how to compare a stringvalue in ng-show inside a customdirective?
问题描述
尝试在其中使用带有ng-show语句的指令.基本上,它会检查字符串的值,该字符串是我的名称" jsonarray中的status_p1属性:
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"
指令定义如下:
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>'
};
})
我的主页中的controller +名称数组如下:
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'
}
]
});
最后是主页:
<body ng-controller="MainCtrl">
<div ng-repeat="name in names">
<radio-button></radio-button>
</div>
</body>
当前是显示一个叉号,它应该显示一个对勾/勾号.我期望条件评估为TRUE,因为status_p1属性等于'正在工作'.如何修改此ng-showstatement以使字符串比较有效? plunkr链接: http://plnkr.co/edit/3VdsbsSHpkNJFVnvkmOW?p=preview
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
推荐答案
表达式
ng-show="name.status_p1==working"
将name.status_p1
与当前作用域中的working
属性进行比较,这在您的情况下未定义.您需要将其与文字字符串'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'";
修改后的 Plunkr
这篇关于如何在customdirective内的ng-show中比较字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!