NG-类不删除一类是有自举前角 [英] ng-class does not remove a class that was there before angular was bootstrapped

查看:121
本文介绍了NG-类不删除一类是有自举前角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不过下72小时想出角度来加速。其采用淘汰赛了一段时间,我不得不说这是有趣的。我的问题,现在有纳克级的事情。

Still under 72 hours coming up to speed with angular. Having used knockout for a while, I have to say it's interesting. My question right now has to do with ng-class.

说我有以下几点:

<div class="myClass" ng-class="{myClass: false}">

当角度引导程序,它不删除那是以前呈现那里的MyClass的属性。我是有希望的,因为这是KO的CSS绑定什么呢。似乎只角删除类,如果是角把它放在那儿谁的人。

When angular bootstraps, it does not remove the myClass attribute that was rendered there before. I had expected it to, since that's what ko's css binding does. It seems that angular only removes the class if angular was the one who put it there.

那么,有没有一个常见的​​解决方法吗?除了创建一个自定义指令?

So, is there a common workaround for this? Other than creating a custom directive?

推荐答案

由于您只采用了棱角分明的形式和需要一类是活跃在角的Bootstrap它看起来像一个指令可能是之前的表单中的元素最好的路要走。

Since you're only using Angular for the form and need a class to be active on elements within the form prior to Angular bootstraping it looks like a directive may be the best way to go.

下面的指令将从元素中删除指定的类对曾经的指令是由角(这几乎是在 ngClass 将踢在同一个点)挂在

The following directive will remove the specified class from the element its on once the directive is linked in by Angular (which is almost the same point when ngClass will kick in):

.directive('removeClass', function(){
    return {
        restrict: 'A',
        link: function(scope,element, attrs){
            element.removeClass(attrs.removeClass);
        }
    };
});

用于像这样:

<div class="oldClass" remove-class="oldClass" ng-class="{newClass: true}">stuff</div>

演示小提琴

这篇关于NG-类不删除一类是有自举前角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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