Ola Zeyad
Ola Zeyad

Reputation: 113

how to reset select in angular2

I have tow select lists (select1 and select2) in my form, select2 depends on select2 selection, when select1 selection change options in select2 must be change this work fine but my problem is that on change don't return to default option, it gives me last index of last select2 selection, i want on change the select2 option change and index of selected option return to 0 or default option I am using ng-model to deal with select in angular2

this my html code:

<td *ngIf="classificationCheck"><select name="classification_type" [(ngModel)]="message.classification_type" 
                    class="form-control" [disabled]="isReply||isForward">
                            <option value="" disabled default>تصنيف موضوعي</option>
                            <option *ngFor= "let subType of classificationTypes">{{subType}}</option>
                    </select></td>

and this onChange function for select1 witch update select2 options:

classificationChange( event ){

    if("غير محدد"==event){
        this.classificationCheck=false;
        this.message.classification_type=null;
    }
    else{
        for(let classtype of this.classifications){

            if(classtype.gen==event){
                if(classtype.name)
                    {
                        this.classificationTypes=[];
                        this.classificationCheck=true;

                        for(let subType of classtype.name){
                            this.classificationTypes.push(subType);
                        }
                    }
            }

        }
    }

}

Upvotes: 3

Views: 2023

Answers (1)

danday74
danday74

Reputation: 57175

Put an *ngIf on your select. Set it to true. When you want to reset, set it to false then back to true very quickly.

This was the official approach in RC4 until a proper approach was made available.

If a new approach is now available in the final release of ng2, I don't know.

Upvotes: 0

Related Questions