简体   繁体   English

如何在Angular对象数组内部执行函数

[英]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, 现在可以使用OutputEvent 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? 用一个更具体的词来说,有什么办法可以在同一个数组对象中获得回调?

Stackblitz Demo Stackblitz演示

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.

相关问题 如何访问 Angular 中对象数组中的数组? - How to access array inside of an array of objects in Angular? 如何访问Angular 4中另一个对象数组中的对象数组 - How to access an array of objects inside another array of objects in angular 4 如何在JavaScript中的函数中访问数组对象? - How to access array objects inside a Function in javascript? 如何在Angular JS的对象数组内求和货币值 - How to sum up currency values inside an array of objects in Angular JS 如何基于对象数组中的元素使用角度滤镜 - how to use angular filter based on the elements inside array of objects 如何使用 useState function 组件更新对象数组中的数组 - How to update an array inside an array of objects with useState function component 如何在数组Angular中执行observable? - how to execute observable in array Angular? 我如何访问javascript函数返回的数组中的对象 - How do i access the objects inside the array returned by a javascript function 如何使用 reduce function 查找对象数组中字段的值? - How to use reduce function to find the value of field inside an array of Objects? 如何编写返回数组内的对象作为输出的 Javascript function - How to write a Javascript function that returns objects inside an array as outputs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM