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

前端 未结 7 1691
北荒
北荒 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:16

    I was struggling with the same question and put my solution in this article:

    http://almerosteyn.github.io/2016/02/angular15-component-typescript

    module app.directives {
    
      interface ISomeComponentBindings {
        textBinding: string;
        dataBinding: number;
        functionBinding: () => any;
      }
    
      interface ISomeComponentController extends ISomeComponentBindings {
        add(): void;
      }
    
      class SomeComponentController implements ISomeComponentController {
    
        public textBinding: string;
        public dataBinding: number;
        public functionBinding: () => any;
    
        constructor() {
          this.textBinding = '';
          this.dataBinding = 0;
        }
    
        add(): void {
          this.functionBinding();
        }
    
      }
    
      class SomeComponent implements ng.IComponentOptions {
    
        public bindings: any;
        public controller: any;
        public templateUrl: string;
    
        constructor() {
          this.bindings = {
            textBinding: '@',
            dataBinding: '<',
            functionBinding: '&'
          };
          this.controller = SomeComponentController;
          this.templateUrl = 'some-component.html';
        }
    
      }
    
      angular.module('appModule').component('someComponent', new SomeComponent());
    
    }

    0 讨论(0)
提交回复
热议问题