angular's ng-init alternative in Angular 2

后端 未结 8 897
遇见更好的自我
遇见更好的自我 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:59

    You do not always need a custom directive for this. If you're okay with your function being called more than once, you can simple do:

    The word "init" there is completely arbitrary. The downside is yourInitFunction will get called on every digest cycle.

    Note, if you return anything from your function this will add an attribute called "init" to your element with the returned value. If you return undefined, it will not add the attribute.

    This is normally a non-issue, just keep it in mind.

提交回复
热议问题