我如何使用ngClass AngularJS过滤器?过滤器、如何使用、ngClass、AngularJS

由网友(旧梦滥心 ミ       )分享简介:我curretly学习AngularJS和教程播放。I'm curretly learning AngularJS and playing with the tutorial.我修改教程示例过滤器返回一些字符串:I'm modifying the tutorial example filter to return...

我curretly学习AngularJS和教程播放。

I'm curretly learning AngularJS and playing with the tutorial.

我修改教程示例过滤器返回一些字符串:

I'm modifying the tutorial example filter to return some string:

angular.module('phonecatFilters', []).filter('checkmark', function() {
  return function(input) {
    return input ? 'true-class' : 'false-class';
  };
});

和我想使用在 ngClass 如下:

{{phone.trueVal  | checkmark}} <i ng-class="{{phone.trueVal  | checkmark }}"></i>
{{phone.falseVal | checkmark}} <i ng-class="{{phone.falseVal | checkmark }}"></i>

结果为:

true-class <i class="false-class">
false-class <i class="false-class">

现在..而对于简单视图过滤器工作正常..为什么它不适合ngClass工作?什么对子级是使用 ngClass (如 ngSrc 等等等)滤波值的正确方法。

Now.. while for simple view the filter works as expected.. why does it not work for ngClass? What whould be the correct way to use a filtered value in ngClass (and other like ngSrc etc).

推荐答案

在你的情况,你应该使用而不是纳克级,因为您的渲染直接在HTML类名的没有评估。

In your case, you should use class instead of ng-class because you render the class name directly on the html without evaluation.

<div ng-controller="MyCtrl">
   <i class="{{phone.trueVal  | checkmark }}">{{phone.trueVal  | checkmark}}</i>
   <i class="{{phone.falseVal | checkmark }}">{{phone.falseVal | checkmark}}</i>
</div>

演示

阅读全文

相关推荐

最新文章