如何在自定义指令中比较 ng-show 中的字符串值? [英] how to compare a stringvalue in ng-show inside a customdirective?
问题描述
尝试使用包含 ng-show 语句的指令.基本上它检查字符串的值,该字符串是我的名称"jsonarray 中的 status_p1 属性:
ng-show="name.status_p1==working"
指令定义如下:
app.directive('radioButton',function(){返回 {限制:'E',替换:'真',模板:'<table border="2px">'+'<tr><td>{{name.name}}</td><td>工作</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>'+'</表>'};})
我主页中的控制器+名称数组如下所示:
app.controller('MainCtrl', function($scope) {$scope.names = [{name: '情侣 1',status_p1: '工作',status_p2: '退休'}]});
最后是主页:
<div ng-repeat="name in names"><单选按钮></单选按钮>
当前显示一个十字,它应该显示一个勾号/勾号.我期望条件评估为 TRUE,因为 status_p1 属性等于工作".如何修改此 ng-showstatement 以使字符串比较有效?plunkr 链接: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
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
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
这篇关于如何在自定义指令中比较 ng-show 中的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!