angular5 子组件监听父组件传入值的变化

项目中遇到一个问题,就是在ngInit()中调用方法,只调用一次的问题,当父组件传值变化时,并不会再次执行(ps:csdn又抽风了,代码格式被清除了)。

import {

Component,

Input,

OnChanges,

SimpleChanges

} from '@angular/core';

import {

NavController

} from 'ionic-angular';



@Component({

selector: 'cs-img-lazy',

templateUrl: 'cs-img-lazy.html'

})

export class CsImgLazyComponent implements OnChanges {



@Input() default: string; 

@Input() src: string 



constructor(public navCtrl: NavController) {



}



ngOnInit() {

}



ngOnChanges(changes: SimpleChanges) {

   console.log('ngOnChanges', this.src);

   this.loadImage(this.src, () => {

      this.default = this.src;

   });

}



loadImage(url, callback) {

var img = new Image();

img.onload = () => {

if (img.complete) {

img.onload = null

callback(img);

return;

}

}

img.src = url;

}




}

 

你可能感兴趣的:(Angular)