У меня есть HTML файл, который использует подход "контроллер как". Он имеет сложный тип свойства (массив объектов), который я хочу представить пользователю с помощью угловой директивы, чтобы я мог использовать его и на других страницах. Моя директива также использует подход "контроллер как".
--- 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 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: "="}?
Что мне не хватает? Что-то в ссылке (возможно)?
Проблема заключалась в том, что в моей директивной регистрации я отсутствовал следующее свойство
bindToController: true,
пример
return {
restrict: "EA",
templateUrl: "/app/directives/documentReference/documentReference.html",
scope: {
documentReferenceStructure: "="
},
controller: "documentReferenceController",
controllerAs: "controller",
bindToController: true
}