[英]How to execute function inside array of objects in Angular
mentioned below: 如以下所说的:
users = [
{
name: 'Test User',
onSelected: function (par1, par2) {
// some operation with par1, par2
alert('Test User');
}
},
{
name: 'Test User 2',
onSelected: function (par1, par2, par3) {
// some operation with par1, par2, par3
alert('Test User 2');
}
},
{
name: 'Test User 3',
onSelected: () => {
this.router.navigate(['/some/path', 12])
.then();
}
}
];
I pass this array to my child component with the help of inputs decorator, as shown below, app.component.html: 我在输入装饰器的帮助下将此数组传递给子组件,如下所示, app.component.html:
<render-users [users]='users' (selectedUser)="selectedUserManager($event)"></render-users>
Now Am able to emit the selected user using Output
and Event Emitter
, as shown below, 现在可以使用
Output
和Event Emitter
选定的用户,如下所示,
render-users.component.html: render-users.component.html:
<div
*ngFor="let user of users"
(click)="setSelectedUser(user)">
</div>
render.component.ts: render.component.ts:
setSelectedUser(user: User): void {
this.selectedUser.emit(user);
}
My question is how do I execute a function inside the selected Object? 我的问题是如何在所选对象内执行功能? Here, I can call
$event.onSelected()
in app.compoent.ts file as show below, 在这里,我可以在app.compoent.ts文件中调用
$event.onSelected()
,如下所示,
app.component.ts: app.component.ts:
selectedUserManager($event) {
$event.onSelected()
}
But in the case of a different function parameter, how do manage without switch case inside app.component.ts 但是在使用不同功能参数的情况下,如何在app.component.ts内不切换大小写的情况下进行管理
In NgFor Loop whenever I select any particular user, the function attached to onSelected Key should be executed. 在NgFor Loop中,每当我选择任何特定用户时,都应执行附加到onSelected Key的功能。
Here I can't use a switch case mechanism, instead, I have to execute the function of that selected user. 在这里,我不能使用切换用例机制,而是必须执行所选用户的功能。
In a more specific word is there any way I can get a callback within the same array object? 用一个更具体的词来说,有什么办法可以在同一个数组对象中获得回调?
If I have left put out any extra details please let me know and thanks in advance. 如果我遗漏了任何其他详细信息,请让我知道并提前致谢。
forked your stackbliz 分叉你的堆栈
https://stackblitz.com/edit/angular-memv6o?file=src%2Findex.html https://stackblitz.com/edit/angular-memv6o?file=src%2Findex.html
Passed three parameters and all are not required in function defination 通过了三个参数,函数定义中不需要所有这些参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.