How to add an attribute using a binding expression from an internal link function

How to add an expression to a parameter elementpassed to a directive function link?

For example, inside the directive I want to add this:
element.setAttribute('ng-disabled','{{disabled}}');

If the property is disabled in the directive scope.

There is no template in the directive, it just uses the parameter elementpassed to.

I think I should recompile the element using the scope of the directive, but I'm not sure if this will recompile the directive either.

I know that I can disable $watchand disable the disabled property for an element, but I'm trying to figure out if I can add dynamic attributes.

Thank.

+3
source share
2 answers

someValue .

:

parameter="someValueCanPassTo"

:

scope: {
    parameter: '='
},
link: function(scope, element, attrs) {
    $watch('parameter', function() {
        element.attr('my-attr', scope.parameter);
    });
}
+1

, element.attr('attrName', 'attrVal'). , , :

$scope.$watch(attrs.attrName, function(val){
  element.attr('disabled', val);
});

, ( ).

, ng-disabled, , .

0

All Articles