简体   繁体   English

指令链接功能中的AngularJS更新属性

[英]AngularJS Updating attribute in directive linking function dynamically

I have a directive that receives a string, which I would like the directive to then modify and add as an attribute's property. 我有一个接收字符串的指令,我希望该指令随后进行修改并添加为属性的属性。

HTML 的HTML

<div directive ng-class="" otherAttribute="hello"></div>

JS JS

.directive('directive', function () {

return {

    link: function (scope, elem, attrs) {

            var classList = "{active: attrs.otherAttribute == 'hello'}";

            attrs.ngClass = classList;

            console.log(attrs);

        }

    }

})

This does seem to add the properties to the class property of the attrs object: 这似乎确实将属性添加到attrs对象的class属性中:

console.log(attrs);

$$observers: Object
$attr: Object
ngClass: "{active: attrs.otherAttribute == 'hello'}"
otherAttribute: "hello"
__proto__: Object

But the DOM is not updated. 但是DOM不会更新。 I've tried $compile , but that does not update the class attribute on the DOM either. 我已经尝试过$compile ,但这也不会更新DOM上的class属性。

What am I missing? 我想念什么? Many thanks! 非常感谢!

You can use the directive element to modify a css class. 您可以使用指令元素来修改css类。

Working example of your code: http://jsfiddle.net/egrendon/vvnu5yLx/1/ 您的代码的工作示例: http : //jsfiddle.net/egrendon/vvnu5yLx/1/

app.directive('directive', function () {
    return {
        link: function (scope, element, attrs) {
            scope.classList += "class1 class2";
            element.addClass(scope.classList);
            console.log(attrs);
        }
    }
});

You can use interpolation and reference directive scope 您可以使用插值和引用指令范围

 <div directive class="class1 {{classList}}" otherAttribute="hello"></div>

link: function (scope, elem, attrs) {
        scope.classList = "class2";
}

Pay attention that in your code there is an error, you reference an undefined scope.classList with an += operator, resulting in undefinedclass1 class2 literal. 请注意,在您的代码中有一个错误,您使用+=运算符引用了一个未定义的scope.classList ,从而导致了undefinedclass1 class2文字。 I suppose this might be a typo in example... 我想这可能是示例中的错字...

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

相关问题 在AngularJS中将属性伪指令动态添加到包含伪指令 - Dynamically adding an attribute Directive to a transclude Directive in AngularJS AngularJS中多个属性指令的编译和链接顺序? - Order of compiling and linking for multiple attribute directive in AngularJS? 触发属性作为angularjs指令的函数 - Trigger an attribute as function of angularjs directive AngularJS Material在指令链接函数中使用$ mdDialog - AngularJS Material using $mdDialog in a directive linking function AngularJS指令:将动态创建的对象作为嵌套指令的属性传递 - AngularJS Directive: Pass dynamically created object as attribute for nested directive AngularJS不更新,指令中的指令 - AngularJS not updating, directive in directive AngularJS指令 - 将函数作为参数传递给“&”属性 - AngularJS directive - passing function as argument to “&” attribute AngularJS:在指令的链接函数中查看异步值 - AngularJS: Watching for async values within directive's linking function AngularJS指令将指令添加为属性并动态绑定它们 - Angularjs directive add directives as attribute and bind them dynamically AngularJS观察指令属性表达式,动态继承范围 - AngularJS observe directive attribute expressions, inherit scope dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM