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

on the Page<rn-text-edit rn-scope="profile.first_name"><rn-text-edit>on the jsap

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.

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.

Share Improve this question asked May 1, 2013 at 20:47 simmusimmu 731 silver badge5 bronze badges 1
  • Can the directive use an isolate scope? – Mark Rajcok Commented May 1, 2013 at 21:24
Add a ment  | 

1 Answer 1

Reset to default 6

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745320923a4622438.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信