Is it possible to declare comment inside of ng-class

别等时光非礼了梦想. 提交于 2019-12-10 13:37:08

问题


I have a question is it possible to declare comment inside of ng-class

ng-class="{'test':true,'test1':true, //some comment 'test2':true}"

回答1:


It doesn't appear to. You would have needed to use the multiline comment syntax like so:

<div ng-class="{'test':true,'test1':true, /* some comment */ 'test2':true}"></div>

But that throws an error:

Syntax Error: Token '*' is unexpected, expecting [:] at column 29 of the expression [{'test':true,'test1':true, /* some comment */ 'test2':true}] starting at [* some comment */ 'test2':true}].

However you can work around this by declaring your styles in your controller/directive:

$scope.styles = {
    'test': true,
    'test1': true,
    /* some comment */ 'test2': true
};

And including that in your view:

<div ng-app="MyApp">
    <div ng-controller="MyCtrl">
        <div ng-class="styles"></div>
    </div>
</div>

jsFiddle Example




回答2:


nope, there is no way to comment inside JSON or ng- expressions in HTML, so if you need to coment, use standard HTML somewhere around (not inside tag between params, it will "crash"). Use it before or after tag. But remember, that comments will be visible to users viewing sourcecode of your page. Use some uglify tool before release to remove that comments in production.



来源:https://stackoverflow.com/questions/30523455/is-it-possible-to-declare-comment-inside-of-ng-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!