Triggering change detection manually in Angular

后端 未结 5 999
滥情空心
滥情空心 2020-11-21 11:47

I\'m writing an Angular component that has a property Mode(): string.

I would like to be able to set this property programmatically not in response to a

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 12:14

    In Angular 2+, try the @Input decorator

    It allows for some nice property binding between parent and child components.

    First create a global variable in the parent to hold the object/property that will be passed to the child.

    Next create a global variable in the child to hold the object/property passed from the parent.

    Then in the parent html, where the child template is used, add square brackets notation with the name of the child variable, then set it equal to the name of the parent variable. Example:

    
    
    

    Finally, where the child property is defined in the child component, add the Input decorator:

    @Input()
    public childVariable: any
    

    When your parent variable is updated, it should pass the updates to the child component, which will update its html.

    Also, to trigger a function in the child component, take a look at ngOnChanges.

提交回复
热议问题