Search code examples
angularangular-ngmodelchange

how to assign the value returned from function call to a variable inside html in angular 2


<select [(ngModel)]="country" (change)="stateList = getStateList(country)">
        <option *ngFor="let c of countryList" [value]="c.Country.CountryID"></option>
</select>

Here in (ngModelChange) I want to assign the value returned from getStateList() to a varible, but it just calls the function and the return value is not bound to the variable.


Solution

  • You can simply assign inside the getStateList function itself

    <select [(ngModel)]="country" (change)="getStateList(country)">
            <option *ngFor="let c of countryList" [value]="c.Country.CountryID"></option>
    </select>
    

    inside the component

    getStateList(country : any){
       this.stateList = (getData);
    
    }