【发布时间】:2015-12-28 13:12:24
【问题描述】:
(使用 Angular 1.5)
我正在尝试为我的 Angular 应用程序使用以下设计模式:
angular.module("app").directive("MyDirective", MyDirective);
function MyDirective () {
return {
bindToController: {
someId: "=",
otherAttr: "@"
},
controller: ["$attrs", MyController],
controllerAs: "ctrl"
};
}
function MyController ($attrs) {
var self = this;
console.log(self);
console.log($attrs);
}
我在我的 HTML 中这样使用我的指令:
<my-directive someId="someParentScope.model._id" otherAttr="1">
在控制台中,self 我看到了:
Object { otherAttr: undefined, someId: undefined }
对于$attrs 我明白了:
Object { $attr: Object, $$element: Object, otherattr: "1",
someid: "someParentScope.model._id", otherAttr: undefined, someId: undefined,
$$observers: Object }
我怎样才能完成我想要完成的事情(即将数据从父范围传递到我的指令控制器构造函数),为什么我的单个绑定(“@”绑定)otherAttr 在控制器构造函数中未定义?这种设计模式是否有缺陷/被误导?谢谢!
【问题讨论】:
标签: angularjs angularjs-directive attributes angularjs-scope