2

I have the below HTML in Angular

<span *ngIf="ControllerType?.AttributeID =='Controller Type'">                                            
    <select multiple name="ControllerType.Default" [(ngModel)]="ControllerType1">
        <option *ngFor="let z of ControllerType.Options" value={{z.OptionID}}>
            {{z.OptionID}}
        </option>
    </select>
</span>

What I want to know is how can I set ControllerType.Default which is of type string to get the values selected in ControllerType1. So whatever values were selected and stored in ControllerType1 should also be stored in ControllerType.Default

Doing this {{ControllerType.Default=ControllerType1;""}} after the Select tag and inside the iteration just errors out.

1 Answer 1

1

use the expression inside the ngModelChange

<span *ngIf="ControllerType?.AttributeID =='Controller Type'">
   <select multiple name="ControllerType.Default" [(ngModel)]="ControllerType1"  (ngModelChange)="ControllerType.Default = ControllerType1">
   <option *ngFor="let z of ControllerType.Options" value={{z.OptionID}}>
      {{z.OptionID}}
   </option>
   </select>
</span>

updated

create and ngInit directive and call it in the HTML

@Directive({
  selector: 'ngInit',
  exportAs: 'ngInit'
}) 
export class NgInit {
  @Input() values: any = {};

  @Input() ngInit;
  ngOnInit() {
    if(this.ngInit) { this.ngInit(); }
  }  
}

assign the expression under ngInit directive

[ngInit]="ControllerType1 = ControllerType.Default"

  <span [ngInit]="ControllerType1 = ControllerType.Default"  *ngIf="ControllerType?.AttributeID =='Controller Type'">
       <select multiple name="ControllerType.Default" [(ngModel)]="ControllerType1"  (ngModelChange)="ControllerType.Default = ControllerType1">
       <option *ngFor="let z of ControllerType.Options" value={{z.OptionID}}>
          {{z.OptionID}}
       </option>
       </select>
    </span>
Sign up to request clarification or add additional context in comments.

6 Comments

very cool..how can I load the value back on page refresh so basically do the opposite {{ControllerType1 = ControllerType.Default}} so the selected value that were saved gets displayed.
if you want to keep the values after the page refresh then you need to store if in the localstorage or something and retrieve it at the time page load
Yeah I am storing it in the DB..ControllerType.Default is how it is stored and is what contains the stored values when page refreshes..but how do I map it to ControllerType1 so it displays on the page.
in your export class assign ControllerType1 = ControllerType.Default initially
There are a lot of these select types and then i ll have to do the loop in the export class as well..Is it possible that I can do something like that in the html where I am already iterating.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.