Home  >  Q&A  >  body text

angular.js - angular的ng-class指令和class有什么区别

刚学angular js两天,有一个疑惑,看到angular js中的ng-class指令可以为某一个元素结点添加相应的class。

但是想请问一下,这个跟直接给元素添加class有什么区别吗?angular js最终还是会解析ng-class指令中的内容,然后再给元素添加相应的class。这么做的优势体现在哪呀?就是为了后期方便修改这个元素结点的class吗。

曾经蜡笔没有小新曾经蜡笔没有小新2685 days ago604

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-05-15 17:07:31

    For example: ng-class="{red: By performing operations, you can dynamically add or delete classes, which is much more convenient than using javascript to add or delete classes in the class list

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-15 17:07:31

    ngClass is used to dynamically add different classes based on conditions, but native classes do not have this effect

    reply
    0
  • 迷茫

    迷茫2017-05-15 17:07:31

    ng-class is a command that allows you to change the class based on logic

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 17:07:31

    The value corresponding to the class used in our HTML is a string;
    The ng-xxx in angular corresponds to an expression. For example, the ng-class you encountered here corresponds to a key: value.
    When value is true, the key style is applied.
    For example ng-class="{ someClass : true }";

    Note that if your style is some-class, it needs to be expressed as a string type, such as:

    ng-class="{ 'some-class' : true }";

    reply
    0
  • Cancelreply