Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

angular - ngModel on NbDialog is read only?

I want to open a Dialog from a component by clicking a button, and pass some data to the dialog. When closing the dialog, I want the data to return to the parent component to be sent to a server.

In my parent component I have:

<ng2-smart-table 
  [settings]="settings" 
  [source]="data"                
  (editConfirm)="onEditRowSelect($event)">
</ng2-smart-table>
 
onEditRowSelect(event) {

  this.dialogService.open(MyDialogComponent, {
  context: {
    title: 'Dialog Title',
    data: this.data // this is an Object
  },
  });
}

My dialog is in another component:

  @Input() title: string;
  @Input() data: any;

  constructor(protected ref: NbDialogRef<MyDialogComponent>) {
    console.log(data)
  }

  cancel() {
    this.ref.close();
  }

  submit() {
    this.ref.close({ newData: data });
  }

<nb-card>
....
  <div>
    <input #name type="text" nbInput placeholder="Name" [(ngModel)]="data.user.name">
  </div>
....
</nb-card>
<!-- TEST
<pre>{{data | json}}</pre>
-->

So, the input is prefilled with the "name" that's coming from the parent component, but when I try to modify the name, the "data" model does not change, and I get an error like:

Cannot assign to read only property 'name' of object '[object Object]'

I made it work by deep cloning the Object "data" and using that as the ngModel, then sending it back to the parent as newData... but.. I really don't like this solution, it cannot be the right way to do this.

Any help? THank you

question from:https://stackoverflow.com/questions/65942636/ngmodel-on-nbdialog-is-read-only

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...