1

I am trying to bind dynamic array to md-option. But it is throwing error.

                    <md-select id="hospital0{{index}}" placeholder="Hospital" style="width:100%; " name="hospital">
                      <md-option *ngFor="let hospital of hospitalList{{index}}" [value]="hospital.id">{{ hospital.name }}</md-option>
                    </md-select>

I tried another approach. In that I am fetching the select element and then adding options to it. But it is not adding option inside md-option. This I have tried

    public async GetHospitalForCity(cityId: any) {
console.log(cityId);
let ddl = (<HTMLSelectElement>document.getElementById("hospital000"));
let option = document.createElement("option");
option.value = "1";
option.text = "Hospital1";
ddl.appendChild(option);

}

3
  • Cannot read property 'toUpperCase' of undefined ("" placeholder="Hospital" style="width:100%; " name="hospital"> <md-option [ERROR ->]*ngFor="let hospital of hospitalList{{index}}" [value]="hospital.id">{{ hospital.name }}</md-option> "): ng:///AppModule/MrWeeklyPlanComponent.html@97:37 Parser Error: Unexpected token {, expected identifier, keyword, or string at column 29 in [let hospital of hospitalList{{index}}] in ng:///AppModule/MrWeeklyPlanComponent.html@97:37 ("ame="hospital"> Commented Nov 6, 2017 at 15:13
  • This error I am getting with the first approach. Commented Nov 6, 2017 at 15:13
  • 1
    I don't think you should use something like hospitalList{{index}}, because the template expression (with {{}}) will never be parsed. Instead you should use this property inside your component : hospitalList: Array<any[]>, then you can use *ngFor="let hospital of hospitalList[index]" in your template. Commented Nov 7, 2017 at 10:12

3 Answers 3

1

Though the post is old, I add a similar problem and none of the above solutions solved my problem. But eventually I found out a solution as below.

We can have a function in ts which returns the array on index and bind it in template as below:

HTML:

*ngFor="let hospital of getArray(i+1); let i=index"

ts:

getArray(i: number): any[] {
  if (i === 1) {
     return this.hospital1;
  }else {
     return this.hospital2;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I solved my problem using:

ngFor="let hospital of hospitalList[index]"

as said by Elvynia in comments.

Comments

0

When the array inside the ngFor is async, you use the async pipe built in angular.

Example

<md-option *ngFor="let hospital of hospitalList{{index}} | async" [value]="hospital.id">
    {{ hospital.name }}
</md-option>

3 Comments

i added the example, you have to use the async pipe as i showed to get it working.
Cannot read property 'toUpperCase' of undefined ("" placeholder="Hospital" style="width:100%; " name="hospital"> <md-option [ERROR ->]*ngFor="let hospital of hospitalList{{index}} | async" [value]="hospital.id">{{ hospital.name }}</md-"): ng:///AppModule/MrWeeklyPlanComponent.html@97:37 Parser Error: Unexpected token {, expected identifier, keyword, or string at column 29 in [let hospital of hospitalList{{index}} | async] in ng:///AppModule/MrWeeklyPlanComponent.html@97:37 ("pital">
It is not parsing the placeholder braces {{ }}

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.