Mat-autocomplete - How to access selected option?

前端 未结 3 1856
天涯浪人
天涯浪人 2021-01-01 08:39

I know that [value] stores the selected Object (Offer in my case). According to materials documentation, optionSelected emits an event. I tried

相关标签:
3条回答
  • 2021-01-01 09:06

    You can use it like :

    <mat-autocomplete #auto="matAutocomplete" (optionSelected)='getPosts($event.option.value)'>
    

    WORKING DEMO

    0 讨论(0)
  • 2021-01-01 09:08

    It can we done in two ways

    1) Using onSelectionChange which emits MatOptionSelectionChange from mat-option

    <mat-autocomplete #auto="matAutocomplete">
        <mat-option
          *ngFor="let option of options"
          [value]="option"
          (onSelectionChange)="updateMySelection($event)"
        >
          {{ option }}
        </mat-option>
    </mat-autocomplete>
    

    2) Using optionSelected which emits MatAutocompleteSelectedEvent from mat-autocomplete

    <mat-autocomplete 
      #auto="matAutocomplete"
      (optionSelected)="updateMySelection($event)">
        <mat-option
          *ngFor="let option of options"
          [value]="option"
        >
          {{ option }}
        </mat-option>
    </mat-autocomplete>
    
    0 讨论(0)
  • 2021-01-01 09:25
     <mat-form-field floatLabel="always">
                <mat-label>UTBMS Activity Codes</mat-label>
                <input type="text" placeholder="Activity Codes"  [(ngModel)]="activityCode"  aria-label="Number" matInput [formControl]="utbmsActivityCodesControl"
                  [matAutocomplete]="autoActivityCodes">
                <mat-autocomplete autoActiveFirstOption #autoActivityCodes="matAutocomplete">
                  <mat-option *ngFor="let option of utbmsActivityCodes | async" (onSelectionChange)="setBillingActivity(option)"  [value]="option.code">
                    {{option.name}}
                  </mat-option>
                </mat-autocomplete>
              </mat-form-field>
    
    setBillingActivity(object){
        this.actionData.libraryContent.billingActivityId=object.activityId;
      }
    
    0 讨论(0)
提交回复
热议问题