我是 Angular2 的新手,我一直在努力解决这个问题,但在网上找不到解决方案。
[在离子2上!]
我想使用 ngOnChanges() 所以我可以检测到zonaid的变化:
estados.html:
<ion-item>
<ion-label color="branco">Zonas</ion-label>
<ion-select *ngIf="isTrue2" [(ngModel)]=zonaid>
<ion-option value={{bicho.zonas}} *ngFor="let bicho of idespecie">
{{bicho.zonas}}
</ion-option>
</ion-select>
<ion-select *ngIf="!isTrue2" [(ngModel)]="zonaid">
<ion-option value={{item.id}} *ngFor="let item of itemsZon" >
{{item.codigo}}
</ion-option>
</ion-select>
</ion-item>
idespecie和itemsZon是 .json 文件的一部分
estados.ts
import { Component, Input, SimpleChanges, OnChanges } from '@angular/core';
import { NavController } from 'ionic-angular';
import { IpmaBivService} from '../../app/services/ipmabiv.service'
import { Http } from '@angular/http';
@Component({
selector: 'page-estados',
templateUrl: 'estados.html'
})
export class EstadosPage {
itemsZon: any;
itemsEsp: any;
zonaEscolhida: any;
@Input()
zonaid: string;
idzona: '';
idespecie: any[];
isTrue1:boolean = false;
isTrue2:boolean = false;
constructor(public navCtrl: NavController, private ipmaBivService:IpmaBivService) {
}
ngOnChanges(changes: SimpleChanges){
console.log('ngOnChanges ran');
if(this.zonaid != ''){
this.isTrue1 = true;
this.getJsonZonas(this.zonaid);
}
if(this.especie != ''){
this.isTrue2 = true;
this.getJsonEspecies(this.especie);
}
}
}
ngOnInit(){
this.getDataZonas('zonas')
this.getDataEspecies('especies')
// this.getData('especies')
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { HttpModule, JsonpModule } from '@angular/http';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { EstadosPage } from '../pages/estados/estados';
import { OutrosPage } from '../pages/estados/outros';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
@NgModule({
declarations: [
MyApp,
HomePage,
ListPage,
EstadosPage,
OutrosPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
HttpModule,
JsonpModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
ListPage,
OutrosPage,
EstadosPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
一切都发生在同一个组件(离子页面)中。
我的目标是能够在zonaid更改时运行一个方法。如果您认为有比通过 ngOnChanges 更好的方法,请告诉我
谢谢!