时间:2022-10-01 13:35:39 | 栏目:AngularJS | 点击:次
项目中遇到一个问题,就是在ngInit()中调用方法,只调用一次的问题,当父组件传值变化时,并不会再次执行。
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; } }