angular.js - ng-if 定义一个方法,返回字符串,为什么不对
PHP中文网
PHP中文网 2017-05-15 17:04:14
0
2
507

这样写是对的

<li ng-repeat="(x, y) in item" ng-if="x!='a'"></li>

这样写就是输出全部的,ng-if并没有执行

<li ng-repeat="(x, y) in item" ng-if="test()"></li>

...

$scope.test = function(){
    return "x!='a'"
}
PHP中文网
PHP中文网

认证高级PHP讲师

Antworte allen(2)
漂亮男人

第一你没有把x传进去,第二你返回的是一个字符串,字符串永远是对的。这样写再试试:

<li ng-repeat="(x, y) in item" ng-if="test(x)"></li>

$scope.test = function(x){
    return x!='a';
}
为情所困

告诉你一个概念, ng-if中的东西叫做angular表达式,angular会对这个表达式进行parse。
"x!='a'"其实就是$scope.x != 'a',顺带一提这个$scopeng-repeat产生的scope。
下面的test()当然会变parse成$scope.test()在ngRepeat的scope中没找到方法,所以从父scope中找到了你的方法, 然后你的方法return的是一个字符串, 所以判断永远是true了。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!