3

I have two input field ("name" and "city") and one textarea on my screen. Text area is filled up with some JSON data and data contains a few details like "name", "city", "address", "pin code" etc.

How do I only update "name" or "city" inside textarea when user type something on "name" or "city" input fields.

Is there something to do multiple bindings with textarea ?

Any help would be appreciated.

1
  • Not understand properly....can you create a stackbliz demo for it ! Commented Mar 22, 2021 at 5:27

1 Answer 1

2

There is no multiple binding available for such a scenario, however, you can parse the json every change event, and update relevant fields:

see demo

@Component({
  selector: "hello",
  template: `
    <textarea (change)="updateObj()" [(ngModel)]="objJson"></textarea>
    {{ obj | json }}
  `,
  styles: [
    `
      h1 {
        font-family: Lato;
      }
    `
  ]
})
export class HelloComponent {
  obj = {
    name: "John",
    city: "Chicago"
  };

  objJson: string;

  constructor() {
    this.objJson = JSON.stringify(this.obj);
  }

  updateObj() {
    const tempObj = JSON.parse(this.objJson);
    this.obj.name = tempObj.name;
    this.obj.city = tempObj.city;
    // also possible for a generic update:
    // Object.keys(this.obj).forEach(k => { this.obj[k] = tempObj[k]; });
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I got my answer that there is no multiple binding available for such a scenario at the moment.

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.