简体   繁体   English

Angular指令仅在一个指令上更新类

[英]Angular directive only updating class on one directive

I created a directive to add classes to my table filters th elements. 我创建了一个指令来向我的表中添加类过滤元素。

app.directive('tableFilter', function () {
return {
    link: function (scope, element, attrs) {
        element.html(attrs.text + ' <i class="fa fa-unsorted"></i>');
        icon = element.children();


        scope.$watch('reverse', function (oldVal, val) {
            console.log(icon);
            if (angular.isDefined(oldVal)) {
                console.log(val);
                if (val) {
                    icon.addClass('fa-sort-asc');
                    icon.removeClass('fa-sort-desc');
                } else {
                    icon.removeClass('fa-sort-asc');
                    icon.addClass('fa-sort-desc');
                }
            }
        });
    }
}
});

I have 2 th's where I use this directive. 我有2个使用此指令的地方。 The HTML is as follows. HTML如下。

<tr>
    <th table-filter ng-click="tableFilter = 'name'; reverse=!reverse" text="Name"></th>
    <th table-filter ng-click="tableFilter = 'created'; reverse=!reverse" text="Date"></th>
</tr>

If I click on a th the class will be added and removed, but only to the th where text="Date". 如果我点击该类,将添加和删除该类,但仅限于其中text =“Date”。 Everything goes right, except that the directive only adds or removes the class on the last th. 一切顺利,除了指令只在最后一个添加或删除类。

It is possible that this has something to do with the element.children()? 这有可能与element.children()有关吗?

Hope you can help me. 希望您能够帮助我。

Thanks in advance! 提前致谢!

It is because of the icon = ... . 这是因为icon = ...

Defining it that way, makes icon a global, so every time you call icon = element.children() , icon will be updated with that element's children. 以这种方式定义,使icon成为全局icon ,因此每次调用icon = element.children()icon都将使用该元素的子元素进行更新。

You should make it a local variable, like this: 你应该把它变成一个局部变量,如下所示:

var icon = ...;

<fun-spoiler> <乐趣扰流>
Not directly related to your question, but there are still several problems with your implementation. 与您的问题没有直接关系,但您的实施仍存在一些问题。
Eg: 例如:
1. The arguments to $watch() 's callback are reversed (as Sunil D mentioned), ie newVal comes before oldVal . 1. $watch()回调的参数被反转(如Sunil D提到的),即newValoldVal之前。
2. Upon clicking on a th , all icons are updated (which won't make much sense to the user as they won't know which column the table is sorted by). 2.点击th ,所有图标都会更新(这对用户来说没有多大意义,因为他们不知道表格按哪个列排序)。
</fun-spoiler> </乐趣的扰流>


<major-fun-spoiler> <大芬扰流>
Demo implementation . 演示实施
( Note: It is not a good idea to have a reusable component such as table-filter share the scoe with its parent. It introduces tight coupling. You should use an isolate scope and create 2-way data-binding for tableFilter and reverse . But that's another story...) 注意:让一个可重用的组件如table-filter与其父组件共享scoe并不是一个好主意。它引入了紧耦合。你应该使用一个隔离范围并为tableFilterreverse创建双向数据绑定。但那是另一个故事...)
</major-fun-spoiler> </主要芬扰流>

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

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