Когда вы используете геттер, это происходит несколько раз в течение жизни компонента.
Вы должны изменить свой подход. Одним из способов является подписка на valueChanges формы и изменение «состояний» в подписке. Некоторые как
export class StateComponent {
_studentForm; //Use a _studentForm
@Input()
set studentForm(value) //use a setter, to subscribe when has value
{
this._studentForm=value;
this._studentForm.get(this.countryId).valueChanges.subscribe(res=>{
var val = this._studentForm.controls[this.countryId].value;
this.states=this.selectService.filterStates(val);
})
}
@Input() id:string;
@Input() countryId:string;
states: State[];
constructor(private selectService: SelectService) { }
}
Ваш component.html должен быть ссылкой на _studentForm
<form [formGroup]="_studentForm">
<select [formControlName]="id" >
<option [value]="0">--Select--</option>
<option *ngFor="let state of states " value= {{state.id}}>{{state.name}}</option>
</select>
</form>
Ваш разветвленный стек
Обновление
Что ж, учитывая видение всей проблемы, пора создать компонент, который контролирует страну и штаты в определенное время. это более сложное дело, потому что мы должны использовать viewProviders и FormGroupDirective.
Идея, передать в качестве аргумента компоненту имя элемента управления (countryID и StateID) и метку элемента управления (countryLabel и stateLabel)
Новый компонент становится
@Component({
selector: 'app-country-state',
viewProviders: [
{
provide: ControlContainer,
useExisting: FormGroupDirective
}
],
templateUrl: './country-state.component.html',
styleUrls: ['./country-state.component.css']
})
export class CountryStateComponent implements OnInit, OnDestroy {
@Input() countryID: string;
@Input() stateID: string;
@Input() countryLabel: string;
@Input() stateLabel: string;
_countryID: FormControl; //We must control two "FormControl"
_stateID: FormControl; //One for country and one for stated
states: any[] = [];
countries: any[] = [];
isAlive: boolean = true;
constructor(private selectService: SelectService,
private fgd: FormGroupDirective) { }
ngOnInit() {
//first, we get the countries
this.countries = this.selectService.getCountries();
//"search" the controls using the FormGroupDirective
this._countryID = (this.fgd.form.get(this.countryID) as FormControl);
this._stateID = (this.fgd.form.get(this.stateID) as FormControl);
//Our subscribe to valueChanges. We use a "tipical" contruction "takeWhile"
//To unsubscribe when the compnent are destroyed
this._countryID.valueChanges.pipe(
takeWhile(() => this.isAlive)
).subscribe(res => {
this.states = this.selectService.filterStates(this._countryID.value);
})
}
ngOnDestroy() {
this.isAlive = false;
}
}
.html
{{countryLabel}}:<br/>
<!--see that we use [formControl], NOT [formControlName]-->
<select [formControl]="_countryID">
<option [value]="0">--Select--</option>
<option *ngFor="let country of countries" [value]="country.id">{{country.name}}</option>
</select>
<br/>
{{stateLabel}}:<br/>
<select [formControl]="_stateID" >
<option [value]="0">--Select--</option>
<option *ngFor="let state of states " value= {{state.id}}>{{state.name}}</option>
</select>
И польза
<app-country-state
[countryID]="'countryId1'" [countryLabel]="'Student Country'"
[stateID]="'stateId1'" [stateLabel]="'Student State'">
</app-country-state>
Разветвленный стек