我如何在NG-类的字符串比较? [英] How do I compare with a string in ng-class?

查看:228
本文介绍了我如何在NG-类的字符串比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行似乎并没有为我工作。

This line doesn't seem to work for me.

Sort By: <a href="" ng-click="setOrder('title')" ng-class="{active: orderProp == 'title'}">Alphabetical</a>

我必须逃避orderProp =='标题''标题'不知何故?

Do I have to escape 'title' in orderProp == 'title' somehow?

在控制器我有

...
$scope.orderProp = 'title';
$scope.setOrder = function(sortBy){
            $scope.orderProp = sortBy;
}
...

感谢您

更新: 使用V1.3.0-beta.17

Update: Using v1.3.0-beta.17

添加纳克级={活跃:orderProp =='PAGETITLE'}

<a href="" ng-click="setOrder('pagetitle')" ng-class="{active:orderProp=='pagetitle'}">Alphabetical</a>

抛出一个错误

"Error: [$parse:syntax] http://errors.angularjs.org/1.3.0-beta.17/$parse/syntax?p0=undefined&p1=not%20a%20primary%20expression&p2=null&p3=%7Bactive%3AorderProp%3D%3D&p4=%7Bactive%3AorderProp%3D%3D

很抱歉的方式,我present错误,但我刚开始在上周的角度,不知道有更好的方式

Sorry for the way I present the error but I just started angular last week and don't know a better way

更新2:
错误似乎是从=== ===到来。我试过>并没有出错。是否有替代语法喜欢情商?

Update 2: error seems to be coming from = == ===. I tried > and no error occured. Is there an alternative syntax to like eq?

更新3解决

我映射每个字符串一个int pagetile-> $ C $ 1 C-> 2 +数据-NG-CLASS ={活跃:orderPropIdx == 1};
控制器内部我只是做,如果PAGETITLE设置为有效:orderPropIdx 1等等

I mapped each string to an int pagetile->1 code->2 + data-ng-class="{active:orderPropIdx==1};" Inside the controller I just do if pagetitle set active:orderPropIdx to 1 and so on

也许这是一个错误的角度1.3

Maybe this is a bug in angular 1.3

推荐答案

由于在评论中指出,类的名字应该用单引号括起来。

As is stated in the comments, your class name should be surrounded by single quotes.

纳克级={'积极':orderProp =='标题'}&GT;

这比较是区分大小写的。

This comparison is case sensitive.

这篇关于我如何在NG-类的字符串比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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