Is it possible to declare comment inside of ng-cla

2019-06-19 01:19发布

问题:

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.