4

I have a JSON with structure:

For item

"item": {
        "pk": "123456",
        "title": "Title4",
        "list_fields": [
            {
                "pk": "12345",
                "title": "Selector",
                "type": "SEL",
                "position": 1,
                "locked": true,
                "value": {
                        "pk": 567,
                        "value": "Finished",
                        "position": 3,
                        "color": "FF0000"
                }
            }
        ]           
    },

For its selectors

"field": {
  "pk": "12345",
  "type": "SEL",
  "locked": true,
  "position": 1,
  "values": [
      {
          "pk": 123,
          "value": "Not Started",
          "position": 1,
          "color": "FF0000"
      },
      {
          "pk": 345,
          "value": "In Process",
          "position": 2,
          "color": "FF0000"
      },
      {
          "pk": 567,
          "value": "Finished",
          "position": 3,
          "color": "FF0000"
      }
    ]
}

So, I trying to make a simple selector, where I can see current list_fields values, that a part of field

    <ng-container *ngFor="let list_field of item.list_fields">

            <select [(ngModel)]="list_field.value" [name]="list_field.pk" [id]="list_field.pk">
                <option disabled>Select value</option>
                <option *ngFor="let val of field.values" [ngValue]="val">{{ val.value }}</option>
            </select>
        
    </ng-container>

When I made this selector, I got issue: when page loads, selector is empty (looks like, current value doest bind to select element via [(ngModel)])

selector is empty, but item have initial value

But when I start to select value from select dropdown, selected value bind to item with no problem.

What am I need to do to bind CURRENT value to selector, when page loads?

4
  • you have not set any initial value list_field.value is undefined Commented Dec 22, 2020 at 12:03
  • Initial value was on init. You can see initial value in on screenshot and inside JSON example: {"pk": 567,"value": "Finished","position": 3,"color": "FF0000"} Commented Dec 22, 2020 at 12:05
  • Looks like a problem occurring because I trying to operate with whole object inside selector, but not with a unique value (like pk) inside of it. When I trying to operate with value.pk in selector and inside ngModel - all works fine. But I NEED to operate with a whole object... Commented Dec 22, 2020 at 12:08
  • 1
    if you use ngValue the selected value must base the same object reference Commented Dec 22, 2020 at 12:18

1 Answer 1

1

if you use ngValue as the value of the option this mean you need to set list_field.value as one of the field.values object because the selected value compared base of object reference

ngOnInit(){
this.field.value = this.field.values[2];
}

stackblitz demo 🚀

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, in order to question - its worked, thank you. But model is too flexible to perform such tasks on init: a lot fields may be there. So, my solution was to use value.pk inside selector, detect selector change with (change) and change whole object, using value.pk to find that object inside this.field.values

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.