简体   繁体   English

角方向和ng级

[英]Angular dirrectives and ng-class

Suppose a directive with html eg in component.html like 假设带有html的指令,例如component.html类的

<div class="text-field"
     ng-class="{'classA': varA}">
....
</div>

then I'm trying to do... 那我想做...

<component ng-class="{'classB': varB}"></component>

After that in ng-class I'm having something like {'classB': varB} {'classA': varA} that is obviously not working. 之后,在ng-class我遇到了类似{'classB': varB} {'classA': varA}这样的{'classB': varB} {'classA': varA} ,显然不起作用。

The problem is that I cannot change component.html but I need to add/remove class on keyup (depending on criteria). 问题是我无法更改component.html但需要在keyup上添加/删除类(取决于条件)。 Is it possible to add ng-class in my situation or mayb there's another way to add/remove class? 是否可以在我的情况下添加ng-class或mayb,还有另一种添加/删除类的方法?

Try adding another directive that adds the class via script, something like 尝试添加另一个指令,该指令通过脚本添加类,例如

.directive('myDir',function(){
  return {
    scope:{
     varB:'='
    },
    link: function($scope,$element){
      if($scope.varB)
        $element.addClass('classB');
    }
  }
});

and use like: 并使用像:

<component ng-class="{'classB': varB}" my-dir var-b="something"></component>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM