angular's ng-init alternative in Angular 2

后端 未结 8 880
遇见更好的自我
遇见更好的自我 2020-12-01 12:14

What is the alternative of ng-init=\"myText=\'Hello World!\'\" in Angular 2 to add in the template, not in the component

 
8条回答
  •  臣服心动
    2020-12-01 12:48

    While I agree that initialization should go into the ngOnInit life-cycle hook, it should also be noted that you can use the constructor of the component to initialize class members. In your simple example, you could even use the member declaration to set the variable, e.g.:

    @Component({ template: '
    {{myText}}
    ' }) export class MyComponent { myText = 'Hello World!'; }

提交回复
热议问题