我有一个使用“控制器为”方法的HTML文件。它有一个复杂的属性类型(一个对象数组),我想通过Angular指令呈现给用户,这样我也可以在其他页面上使用它。我的指令也使用“控制器为”方法。
--- Snippet from HTML ---
<body ng-controller="documentViewerController as controller">
Length {{ controller.documentReferenceStructure.length }}
<document-reference document-reference-structure="controller.documentReferenceStructure"></document-reference>
--- Snippet from my directive's template ---
<p>Hello {{ controller.documentReferenceStructure.length}}</p>
--- My directive ---
module myapp.directives {
export class documentReferenceController {
public documentReferenceStructure: IDocumentReferenceStructurePart[];
constructor() {
this.documentReferenceStructure = [];
}
public update() {
this.documentReferenceStructure = [];
}
public static registerDirective(app: ng.IModule) {
app.controller("documentReferenceController", documentReferenceController);
app.directive("documentReference", function () {
return {
restrict: "EA",
templateUrl: "/app/directives/documentReference/documentReference.html",
scope: {
documentReferenceStructure: "="
},
controller: "documentReferenceController",
controllerAs: "controller",
link: (scope, element, attributes, controller : documentReferenceController) => {
}
};
});
}
}
}
当主控制器的documentReferenceStructure属性更新时,网页在“长度”之后的HTML中显示数组的正确长度,但指令不更新并仍然显示“Hello 0”。如果向指令添加一个按钮,以便我可以检查其documentReferenceStructure属性的值,它似乎永远不会更新。
更新页面控制器的documentReferenceStructure属性不会向下传播到指令中,这不是范围:{documentReferenceStructure:“=”}应该做什么?
我错过了什么?链接()中的某些东西也许?
答案 0 :(得分:0)
问题是我在指令注册中缺少以下属性
bindToController:true,
实施例
return {
restrict: "EA",
templateUrl: "/app/directives/documentReference/documentReference.html",
scope: {
documentReferenceStructure: "="
},
controller: "documentReferenceController",
controllerAs: "controller",
bindToController: true
}