Angular 7:ngOnChanges仅触发一次

时间:2019-07-04 14:32:42

标签: angular ngonchanges

我不知道为什么 ngOnChanges 仅触发一次。最初设置时。

import { Component, OnInit, Input, SimpleChanges, OnChanges } from '@angular/core';

@Component({
    selector: 'app-fixed-decimals-input',
    templateUrl: './fixed-decimals-input.component.html',
    styleUrls: ['./fixed-decimals-input.component.scss'],
})
export class FixedDecimalsInputComponent implements OnInit, OnChanges {
    @Input() value: number;

    constructor() {}

    ngOnInit() {}
    ngOnChanges(changes: SimpleChanges) {
        console.log(changes);
    }
}


// html file
    <input name="value" [(ngModel)]="value" />

更改值不会触发 ngOnChanges 有什么想法吗?

1 个答案:

答案 0 :(得分:4)

ngOnChanges仅在@Input()从上级组件更改时触发,而不是在子组件中更改。