【发布时间】:2016-03-02 18:26:36
【问题描述】:
使用 AngularJS 1.5 和 TypeScript 我正在编写一个指令,并希望在我的视图中定义一个属性,该属性被传递到指令中,然后用于输出到模板。我尝试遵循一些示例,例如this one,但我的指令模板未绑定到该属性。
来自页面的 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);
}
【问题讨论】:
标签: angularjs angularjs-directive typescript