2017-03-02 72 views
1

我不能让这段代码的工作,不明白为什么它没有显示该按钮:(不能让角1.5(以打字稿)组件工作

任何想法?谢谢。

https://jsfiddle.net/slishnevsky/Let38jho/10/

角/打字稿

let app = angular.module('app', []); 

export class MyController { 

    public name: string; 

    constructor() {} 

} 

export class MyComponent implements ng.IComponentOptions { 

    public bindings: any; 
    public controller: any; 
    public controllerAs: string; 
    public template: string; 

    constructor() { 
     this.bindings = { 
      name: '@' 
     }; 
     this.controller = MyController; 
     this.controllerAs = 'vm'; 
     this.template = '<button>{{vm.name}}</button>'; 
    } 
} 

app.component('MyComponent', new MyComponent()); 

HTML

<div ng-app='app'> 

    <my-component name='Miranda'></my-component> 

</div> 

回答

1

工作小提琴:https://jsfiddle.net/13ju990x/

两个问题:

  1. 打字稿transpiles的export关键字AMD还是CommonJS的(或UMD)格式。 Look here for a quick example.而且我们没有任何模块加载器在jsfiddle中工作(我认为jsfiddle simpley会将您的代码放入<script>标记中)。您可以简单地打开开发人员工具,并在您的原始代码中查看Uncaught ReferenceError: exports is not defined

  2. 角使用烤肉串案例。所以你需要写app.component('myComponent', new MyComponent());而不是'MyComponenet'。我知道这很愚蠢。