简体   繁体   中英

Angular Material placeholder is not working with @Input

I am using angular materuial. In the parent component I have

<app-multiselect 
    [optionsList]="propertyTypeList"
    [multiple]=true
    [placeholder]="Select"
    ></app-multiselect>

In the child component in the .ts file I am using an @Input

@Input() placeholder: string;

In the child html file I have

<mat-form-field>
  <mat-select 
  [multiple]="multiple"
  [placeholder]="placeholder">
    <mat-option 
    *ngFor="let item of optionsList" 
    [value]="item">{{item}}</mat-option>
  </mat-select>
</mat-form-field>

I am getting "placeholder" instead of "Select".

Any ideas? Thanks :)

In the child HTML, instead of using quotes use double curly bracket:

[placeholder]={{placeholder}}

By using double quotes you are assigning the literal value of the string "placeholder", whereas Angular takes the value of the variable when it's between double curly brackets {{}}.

Since you are passing String as a Input, it needs to be enclosed within ''

<app-multiselect [placeholder]="'Select'" ></app-multiselect>

And when you are binding, you should use string interpolation as {{}}

 <mat-select placeholder="{{placeholder}}">

STACKBLITZ DEMO

I had a similar issue and this worked for me; I got this solution from https://stackblitz.com/edit/angular-mat-select-data-ucmfzw?file=app.component.html

<app-multiselect 
    placeholder="{{'Select'}}"
    ></app-multiselect>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM