5

这是我的指令:

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

4

1 回答 1

14

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

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

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

一般来说,@attributeName. 有关指令的更多信息,请参见此处。

于 2012-10-14T10:37:01.823 回答