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

前端 未结 7 1689
北荒
北荒 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:10

    This is the pattern I use:

    ZippyComponent.ts

    import {ZippyController} from './ZippyController';
    
    export class ZippyComponent implements ng.IComponentOptions {
    
        public bindings: {
            bungle: '<',
            george: '<'
        };
        public transclude: boolean = false;
        public controller: Function = ZippyController;
        public controllerAs: string = 'vm'; 
        public template: string = require('./Zippy.html');
    }
    

    ZippyController.ts

    export class ZippyController {
    
        bungle: string;
        george: Array;
    
        static $inject = ['$timeout'];
    
        constructor (private $timeout: ng.ITimeoutService) {
        }
    }
    

    Zippy.html

    {{vm.bungle}} {{item}}

    main.ts

    import {ZippyComponent} from './components/Zippy/ZippyComponent';
    
    angular.module('my.app', [])
        .component('myZippy', new ZippyComponent());
    

提交回复
热议问题