如何在指令模板中使用传递的数据?

时间:2015-10-30 08:38:17

标签: javascript html angularjs

我创建了一个带有指令的可重用组件,如下所示:

count

menu.html:

app.directive("menu", function () {
    return {
        restrict: 'AE',
        templateUrl: '/menu.html',
        scope: {
            title: '=title'
        }
    };
});

显然我想从外面传递<div>{{title}}</div> 属性,并且显示在title内。

在main.html中的用法:

menu.html

结果:标签未解决。为什么呢?

2 个答案:

答案 0 :(得分:3)

你必须这样声明:

app.directive("menu", function () {
    return {
        restrict: 'AE',
        templateUrl: '/menu.html',
        scope: {
            title: '@'
        }
    };
});


Text Binding    (Prefix: @)
One-way Binding (Prefix: &) (for functions)
Two-way Binding (Prefix: =)

This SO post更深入地了解@ vs =

答案 1 :(得分:0)

有三种类型的指令绑定:

  1. &#34; @&#34; (文本绑定/单向绑定)
  2. &#34; =&#34; (直接模型绑定/双向绑定)
  3. &#34;&安培;&#34; (行为绑定/方法绑定)
  4. 所以在你的情况下,如果你只关心显示文本,你会使用&#39; @&#39;符号