0

I want to add a dropdown list from XML/JSON file. I am able to add dropdown dynamically using the add button but I want to update dropdown programmatically from the backend. My code for adding dropdown dynamically.

blankRow()
{
   ID1: string;
   ID2: string;
}

taglist: Array<any> = [       
      {tag : "test_string"},
      {tag : "test_string1"},
      {tag : "test_string2"},
      {tag : "test_string3"},
      ];

const blankRow= new blankRow();
        blankRow.ID1= this.taglist[0],
        blankRow.ID2 = this.taglist[1],
        
        this.mypage.DataList.push(blankRow); 

HTML code:

   <select [(ngModel)]="mypage.DataList[i].ID1" [ngModelOptions]="{standalone: true}">   
     <option *ngFor="let x of taglist; trackBy:trackByIndex; " [id]="x"  [ngValue]="x">{{x.tag}}</option>
   </select>

with the above logic, I am able to add dropdown using add button but same I want to add from backend from another component as below:

from another complement doing the same thing:

taglist: Array<any> = [       
    {tag : "test_string"},
    {tag : "test_string1"},
    {tag : "test_string2"},
    {tag : "test_string3"},
    ];

const blankRow= new blankRow();
      blankRow.ID1= this.taglist[0],
      blankRow.ID2 = this.taglist[1],
        
     var temp = getdatalist();
     temp.push(blankRow);
     setdatalist(temp); 

I have subscribed to changes on Datalist and with the above code, the dropdown is added but text inside is coming empty. as below. However, when put console logs, I can see data accurately coming into the console. this is an issue, an empty box is coming

I know the issue might be a reference lost when updating data from another source, but I don't know the solution. I have tried track by option as well. Help is appreciated, thanks in advance.

2
  • Please reproduce your issue using Plunker or Stackblitz. Commented Jul 20, 2020 at 10:18
  • 1
    You are trying to assign this.taglist[0] which is an Object to blankRow.ID1 which you expect to be a string? Try assigning this.taglist[0].tag instead Commented Jul 20, 2020 at 10:34

1 Answer 1

1

Change to below code.

  blankRow.ID1= this.taglist[0].tag,
  blankRow.ID2 = this.taglist[1].tag,
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. Actually I need to push object itself with its tag so I am using this.tagist[0]. Backend logic I have that will read the string and assign it. As I already mentioned, the same logic works when I use the plus button (to add dynamic table entries) and add row dynamically. problems occur only when I push array programmatically from the backend.

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.