this post을 따라 Angular의 1.5 구성 요소 postLink 이벤트를 알게되었습니다.
나는 plunker에서 일하고있다.
controller: function() {
this.$onInit = function() {
console.log("$onInit");
this.tabs = [];
};
this.addTab = function addTab(tab) {
console.log("addTab");
this.tabs.push(tab);
};
this.selectTab = function selectTab(index) {
for (var i = 0; i < this.tabs.length; i++) {
this.tabs[i].selected = false;
}
this.tabs[index].selected = true;
};
this.$postLink = function() {
console.log("$postLink. nr of tabs added: " + this.tabs.length);
this.selectTab(this.selected);
};
}
콘솔 출력 :
- $의 onInit
- addTab
- addTab
- addTab
- $ POSTLINK 다음 탭 구성 요소에 대한 코드입니다. nr 개의 탭이 추가됨 : 3
그러나 타이프 스크립트에서 동일한 작업을 시도하면 postLink 이벤트가 너무 빨리 트리거됩니다. 탭을 구성 요소에 추가하기 전에 트리거됩니다.
여기에 코드의 일부입니다 : /tabs/tab/tab.component.ts
namespace MainApp {
const mainApp = angular.module("mainApp");
class TabComponent implements ng.IComponentOptions {
public templateUrl: string | ng.Injectable<(...args: any[]) => string>;
public controller: any;
public controllerAs: string;
public transclude: boolean;
public bindings: any;
public require: any;
constructor() {
this.templateUrl = ["rootUrl", (rootUrl) => rootUrl + "app/uitrijregelingBerekening/tabs/tab/tab.html"];
this.controller = TabController;
this.transclude = true;
this.bindings = {
label: "@",
};
this.require = {
tabs: "^^",
};
}
}
mainApp.component("tab", new TabComponent());
}
/tabs/tab/tab.controller.ts
namespace MainApp {
interface ITabBindings {
label: string;
}
export class TabController implements ITabBindings {
public label: string;
private tabs: TabsController;
public tab: any;
constructor() {
}
public $onInit() {
this.tab = {
label: this.label,
selected: false
};
this.tabs.addTab(this.tab);
}
}
}
/탭/탭. component.ts
namespace MainApp {
const mainApp = angular.module("mainApp");
class TabsComponent implements ng.IComponentOptions{
public templateUrl: string | ng.Injectable<(...args: any[]) => string>;
public controller: any;
public controllerAs: string;
public bindings: any;
public transclude: boolean;
constructor() {
this.templateUrl = ["rootUrl", (rootUrl) => rootUrl + "app/uitrijregelingBerekening/tabs/tabs.html"];
this.controller = TabsController;
this.bindings = {
selected:"@",
};
this.transclude = true;
}
}
mainApp.component("tabs", new TabsComponent());
}
/tabs/tabs.controller.ts
namespace MainApp {
export interface ITabsBindings {
selected: number;
}
export class TabsController implements ITabsBindings {
public selected: number;
public tabs: Array<any>;
private scope: any;
static $inject = ["$scope"];
constructor($scope: ng.IScope) {
this.scope = $scope;
}
public $onInit() {
console.log("$onInit");
this.tabs = new Array<any>();
}
public addTab(tab: any) {
console.log("addTab");
this.tabs.push(tab);
}
public selectTab(index: number) {
for (var i = 0; i < this.tabs.length; i++) {
this.tabs[i].selected = false;
}
this.tabs[index].selected = true;
}
public $postLink() {
console.log("$postLink. nr of tabs added: " + this.tabs.length);
this.selectTab(this.selected);
}
}
}
템플릿이 동일합니다.
지금 콘솔 출력은 다음과 같습니다- $의 onInit
- $ POSTLINK. 탭의 NR 추가 : 0
- angular.js : 13920 형식 오류를 : 정의되지 않은
- addTab의 '선택'설정할 수 없습니다 속성을
- addTab
- addTab
내가 여기서 뭔가를 놓치고 있습니까?
자바 스크립트 코드에서는 2 개의 구성 요소와 컨트롤러도 사용하고 있습니다. 설정은 기본적으로 동일합니다. 탭에서 맞춤 콘텐츠를 코드화 할 수 있기를 원하기 때문에 require를 사용하고 있습니다. 그러나 templateUrl이 비동기 적으로로드되는 자식 요소에 대한 설명은 여기에 대한 답변입니다! 실제로, 리터럴 템플릿으로 전환하면 작동합니다! – Stif