What is best practice to create an AngularJS 1.5 component in Typescript?

前端 未结 7 1692
北荒
北荒 2021-01-30 02:20

I am experimenting with the .component() syntax in Angular 1.5.

It seems that the latest fashion is to code the controller in-line in the component rather t

7条回答
  •  一生所求
    2021-01-30 03:09

    If you wanted to completely adopt an Angular 2 approach, you could use:

    module.ts

    import { MyComponent } from './MyComponent';
    
    angular.module('myModule', [])
      .component('myComponent', MyComponent);
    

    MyComponent.ts

    import { Component } from './decorators';
    
    @Component({
      bindings: {
        prop: '<'
      },
      template: '

    {{$ctrl.prop}}

    ' }) export class MyComponent { prop: string; constructor(private $q: ng.IQService) {} $onInit() { // do something with this.prop or this.$q upon initialization } }

    decorators.ts

    /// 
    
    export const Component = (options: ng.IComponentOptions) => {
      return controller => angular.extend(options, { controller });
    };
    

提交回复
热议问题