simmu
simmu

Reputation: 83

Angular JS - Is there a way to pass the directive's attribute to the template

on the Page

<rn-text-edit rn-scope="profile.first_name"></rn-text-edit>

on the js

app.directive("rnTextEdit", function () {
    return {
        restrict: 'E',
        replace: true,
        template:'<span>{{'+rn-scope+'}}</span>'
    }
});

I know I can replace the DOM and access the attribute through link. I wonder if there is a way of passing the directive's attribute to a template.

Upvotes: 4

Views: 4233

Answers (1)

Austin Greco
Austin Greco

Reputation: 33554

If you are just displaying the value:

<rn-text-edit rn-scope="{{profile.first_name}}"></rn-text-edit>

-

app.directive("rnTextEdit", function () {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            rnScope: '@'
        },
        template: '<span>{{rnScope}}</span>'
    }
});

If the directive needs to modify the value, you could use '=' and skip the double curlies.

fiddle

more info on scope and '@' in the Angular Directives page

Upvotes: 6

Related Questions