使用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);
}
谢谢!这解决了我的问题。 –
不客气! –