我如何使用ngClass AngularJS过滤器? [英] How can I use AngularJS filter in ngClass?

查看:688
本文介绍了我如何使用ngClass AngularJS过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我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>

演示

这篇关于我如何使用ngClass AngularJS过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆