简体   繁体   English

Angular JS-有没有办法将指令的属性传递给模板

[英]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 在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. 我知道我可以替换DOM并通过链接访问属性。 I wonder if there is a way of passing the directive's attribute to a template. 我想知道是否存在一种将指令的属性传递给模板的方法。

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 有关范围和'@'更多信息,请参见“ 角度指令”页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM