M
M
microf2015-09-27 14:04:43
Angular
microf, 2015-09-27 14:04:43

And how to substitute a variable in the directive?

Hello. And how to substitute a variable in the directive? I make a directive that changes its template depending on the incoming value.
If you do so, then

var directive = {
            link: link,          
            templateUrl:             
            function (element, attrs) {
              return attrs.templateUrl ? attrs.templateUrl : '/assets/svg/test.svg';
           },
                 
            restrict: 'EA',
            transclude: true
       };

It treats {{{{param.url}} as a link. And this link is equal to param.url = 'assets/svg/test2', and this value does not come. What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-09-27
@microf

the function executes before the compile directive, so the values ​​have not yet been interpolated. What you can do is use the ng-include directive.

return {
    template: '<ng-include src="templateUrl"></ng-include>',
    scope: {},
    link: function (scope, el, attr) {
        attr.templateUrl.$observe(); // логика изменения переменной.
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question