admin管理员组文章数量:1316681
I have created a dialog using Material Design for Angular. In the original example there was only one field, which was bound with one parameter in parent view. I want to create a dialog, that gathers more than just one parameter and return these parameters back to the parent ponent, so I can post it to my API.
How it looks now:
<h1 mat-dialog-title>Create</h1>
<div mat-dialog-content>
<p>Fill in a new pany name</p>
<mat-form-field>
<input matInput [(ngModel)]="data.name">
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Cancel</button>
<button mat-button [mat-dialog-close]="data.name" cdkFocusInitial>Ok</button>
</div>
What I want to achieve:
<h1 mat-dialog-title>Create</h1>
<div mat-dialog-content>
<p>Fill in a new person name</p>
<mat-form-field>
<input matInput [(ngModel)]="data.name">
<input matInput [(ngModel)]="data.surname">
<input matInput [(ngModel)]="data.email">
<input matInput [(ngModel)]="data.mobile">
...
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Cancel</button>
<button mat-button [mat-dialog-close]="WHAT DO I ENTER HERE?" cdkFocusInitial>Ok</button>
</div>
When I removed the [mat-dialog-close]
property, the data never came back to the ponent and couldn't be sent to API. Could you advise on how do I pass back these multiple values?
I have created a dialog using Material Design for Angular. In the original example there was only one field, which was bound with one parameter in parent view. I want to create a dialog, that gathers more than just one parameter and return these parameters back to the parent ponent, so I can post it to my API.
How it looks now:
<h1 mat-dialog-title>Create</h1>
<div mat-dialog-content>
<p>Fill in a new pany name</p>
<mat-form-field>
<input matInput [(ngModel)]="data.name">
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Cancel</button>
<button mat-button [mat-dialog-close]="data.name" cdkFocusInitial>Ok</button>
</div>
What I want to achieve:
<h1 mat-dialog-title>Create</h1>
<div mat-dialog-content>
<p>Fill in a new person name</p>
<mat-form-field>
<input matInput [(ngModel)]="data.name">
<input matInput [(ngModel)]="data.surname">
<input matInput [(ngModel)]="data.email">
<input matInput [(ngModel)]="data.mobile">
...
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Cancel</button>
<button mat-button [mat-dialog-close]="WHAT DO I ENTER HERE?" cdkFocusInitial>Ok</button>
</div>
When I removed the [mat-dialog-close]
property, the data never came back to the ponent and couldn't be sent to API. Could you advise on how do I pass back these multiple values?
-
2
[mat-dialog-close]="data"
– Roberto Zvjerković Commented Jan 25, 2020 at 11:46
3 Answers
Reset to default 3The only thing you have to do is to send the data from the child (modal in this case) to the parent is to pass [mat-dialog-close]="data"
in the button, The parent can take care of the values sent via the data
object, if the values are correctly assigned.
<button mat-button [mat-dialog-close]="data" cdkFocusInitial>Ok</button>
and
data: {name: this.name, role: this.role}
See this StackBlitz for an example. Both values you added are being printed in the console and passed into the parent's ponent passedValues
object, which is printed in the HTML file.
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
console.log('All the data', result);
this.passedValues = result;
});
Imagine you have such case, in referring ponent you can easily use as you wanted
openDialog() {
this.dialog.open(AddChargesComponent, {
data: { name: this.name, surname:this.surname, email: this.email, mobile: this.mobile}
}).afterClosed().subscribe(response => {
console.log(response.name,response.surname, response.email, response.mobile )
})
}
In addition to Roy's answer, you can pass a subset of the data like this:
<button mat-button [mat-dialog-close]="{name: data.name, email: data.email}" cdkFocusInitial>Ok</button>
本文标签: javascriptPassing multiple values from dialog in Angular 8Stack Overflow
版权声明:本文标题:javascript - Passing multiple values from dialog in Angular 8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742000478a2410909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论