【问题标题】:In Angular JS, how do I inject data from directive attribute to template?在 Angular JS 中,如何将指令属性中的数据注入模板?
【发布时间】:2012-10-14 10:20:35
【问题描述】:

这是我的指令:

app.directive("helloWorld", function() {
  return {
    restrict: "E",
    scope: {
      name: "bind"
    },
    template: "<div>a {{name}} a</div>"
  };
});

我是这样使用它的:

<hello-world name="John Smith"></hello-world>

我希望这个页面在我运行时是这样的:

<hello-world>
  <div>a John Smith a</div>
</hello-world>

但由于某种原因,name 没有注入,实际结果是这样的:

<hello-world>
  <div>a {{name}} a</div>
</hello-world>

我缺少什么吗?我正在使用 Angular JS 1.0.2

【问题讨论】:

    标签: javascript templates angularjs


    【解决方案1】:

    范围声明很奇怪。我不确定 "bind" 声明 - 也许它来自以前的版本。

    当前绑定指令属性的语法如下:

    return {
        restrict: "E",
        scope: {
          name: "@name"
        },
        template: "<div>a {{name}} a</div>"
    };
    

    一般来说,@attributeName。在此处查看more information on directives

    【讨论】:

    • 刚刚回答了我自己的问题,然后似乎已经有了答案 :-) groups.google.com/forum/?fromgroups=#!topic/angular/tfArd7DuFxs
    • 我建议添加更多有关范围的信息。 "@name" 绑定到父作用域中的给定属性; "=getName()" 绑定到父作用域中的 Angular 表达式 'getName()' 的值;和 "&doStuff()" 将函数绑定到调用时执行给定角度表达式的范围。
    猜你喜欢
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多