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

sim*_*mmu 4 javascript angularjs angularjs-directive

在页面上

<rn-text-edit rn-scope="profile.first_name"></rn-text-edit>
Run Code Online (Sandbox Code Playgroud)

在js

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

我知道我可以替换DOM并通过链接访问属性.我想知道是否有一种方法可以将指令的属性传递给模板.

Aus*_*eco 6

如果您只是显示值:

<rn-text-edit rn-scope="{{profile.first_name}}"></rn-text-edit>
Run Code Online (Sandbox Code Playgroud)

-

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

如果指令需要修改该值,您可以使用'='并跳过双重curlies.

小提琴

在范围和更多信息'@'角度指令页面