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

前端 未结 7 1829
北荒
北荒 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:14

    I believe one good approach is to use angular-ts-decorators. With it you can define Components in AngularJS like this:

    import { Component, Input, Output } from 'angular-ts-decorators';
    
    @Component({
      selector: 'myComponent',
      templateUrl: 'my-component.html
    })
    export class MyComponent {
        @Input() todo;
        @Output() onAddTodo;
    
        $onChanges(changes) {
          if (changes.todo) {
            this.todo = {...this.todo};
          }
        }
        onSubmit() {
          if (!this.todo.title) return;
          this.onAddTodo({
            $event: {
              todo: this.todo
            }
          });
        }
    }
    

    and then register them in your module using:

    import { NgModule } from 'angular-ts-decorators';
    import { MyComponent } from './my-component';
    
    @NgModule({
      declarations: [MyComponent]
    })
    export class MyModule {}
    

    If you want to check an example of a real application using it, you can check this one.

提交回复
热议问题