2

I have a problem as I don't understand AngularJS directives 100% yet. I have a directive like so,

in my HTML:

<div my-directive="Hello World"></div>

in my Directive JS file:

.directive('myDirective', [function ( ) {
        return {
            restrict: 'A',
            priority: 100,
            templateUrl: 'views/templates/my-directive.html',
            link: function (scope, element, attrs) {
                // I wish to pass attrs.myDirective to the templateUrl
            }
        };
    }]);

and in my templateUrl 'views/templates/my-directive.html':

<h1> {{ attrs.myDirective }} </h1>

Obviously this does work, my directive value is passed to the link function but not to the templateUrl, how can I output the value in my templateUrl? Silly question I know but I'm not sure what the best way to implement this is?

1 Answer 1

1

You can simply assigned this value to scope variable.

  scope.myDirective=attrs.myDirective

app.directive('myDirective', [function ( ) {
        return {
            restrict: 'A',
            priority: 100,
            templateUrl: 'my-directive.html',
            link: function (scope, element, attrs) {
              scope.myDirective=attrs.myDirective
                // I wish to pass attrs.myDirective to the templateUrl
            }
        };
    }]);

Plunker

Sign up to request clarification or add additional context in comments.

2 Comments

I originally did this and it worked, however I was unsure if I was to use the compile feature to pass the attr this to the templateUrl
there is no need of compile here untill you will have dynamic tempalte :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.