使用AngularJS 1.5和TypeScript我正在編寫一個指令,並希望在我的視圖中定義一個屬性,該屬性被傳遞到指令中,然後用於輸出到模板。我試圖遵循一些例子,如this one,但我的指令模板沒有綁定到屬性。從頁面如何使用TypeScript在Angular指令中綁定屬性?
HTML:
<my-directive name="John"></my-directive>
指令:
module myApp.directives {
"use strict";
export class myDirective {
static directiveId = 'myDirective';
scope = {};
bindToController = {
name: '='
};
restrict = 'EA';
controller = controllers.NameController.ControllerId;
controllerAs = 'vm';
template = '<h1>Name: {{ vm.name }}</h1>';
static $inject = [];
constructor() { }
static instance() {
return new myDirective();
}
}
angular.module("myApp").directive(myDirective.directiveId, myDirective.instance);
}
控制器(真的不知道我需要在構造函數什麼?):
module myApp.controllers {
"use strict";
export class NameController {
static ControllerId = "NameController";
name: string;
constructor(name: string){
this.name = name;
}
}
angular.module("myApp").controller(NameController.ControllerId, NameController);
}
謝謝!這解決了我的問題。 –
不客氣! –