简体   繁体   English

如何在Angular中将对象从一个组件传递到另一个组件

[英]How to pass an object from one Component to another in Angular

I am working on angular2 version. 我正在使用angular2版本。 I have a grid in which i have edit button. 我有一个网格,其中有编辑按钮。 I need to implement whenever i click on edit it should display other component and that object should be passed to that component. 每当我单击编辑时,我都需要实现,它应该显示其他组件,并且该对象应该传递给该组件。 My view is as below: 我的看法如下:

  <table class="table table-striped" width="50%">
  <tr> 
     <td>User Name</td>
     <td>Email</td>
     <td>Gender</td>     
     <td></td>
  </tr>
  <tr *ngFor="let user of Users">
     <td>{{user.UserName}}</td>
     <td>{{user.Email}}</td>
     <td>{{user.Gender | transformgender }}</td>
     <td><a  routerLink="/edituser" routerLinkActive="active">Edit</a></td>  
  </tr>
</table>
<a class="btn btn-primary" routerLink="/adduser" routerLinkActive="active">Add User</a>

Now how can a pass user object to other component called edituser.component.ts. 现在如何将用户对象传递给其他名为edituser.component.ts的组件。 Please suggest. 请提出建议。

Instead of this: 代替这个:

<td><a  routerLink="/edituser" routerLinkActive="active">Edit</a></td> 

You need to just bind a funcion to that 'a' tag and do all the navigation and other user selection in that function like this; 您只需要将一个功能绑定到该'a'标记,并像这样在该功能中进行所有导航和其他用户选择即可;

in the users.component.html: 在users.component.html中:

<td><a  (click)="editThisUser()" routerLinkActive="active">Edit</a></td>

in the users.component.ts: 在users.component.ts中:

` `

// add Router to the top of the component
   import { Router } from '@angular/router';
 //...... some other codes....//
 //in the component class 
   constructor(private router: Router) {}
 //and define the function that you bind in the users.component.html
   editThisUser(): void {
     this.router.navigate(['/edituser', this.selectedUser.id]);
   }`

for further information i suggest you to check the official angular heroes tutorial: https://angular.io/docs/ts/latest/tutorial/toh-pt6.html 有关更多信息,我建议您查看官方的《角力英雄》教程: https : //angular.io/docs/ts/latest/tutorial/toh-pt6.html

Setup a very basic service that hold data? 设置一个非常基本的服务来保存数据? :- :-

import { Injectable } from '@angular/core';
import { Component, OnInit } from '@angular/core';

@Injectable()
export class UserSelectedService {
    public userSelected: any = {}; 
}


@Component({
    selector: 'user-view',
    templateUrl: 'user-view.html',
    styleUrls: ['user-view.scss']
})

export class UserViewComponent implements OnInit {

    public user: any;

    constructor(private _userSelectedService: UserSelectedService) {}

    public ngOnInit() {
        this.user = this._userSelectedService.userSelected;
    }

}

Then edit html so that when clicked to set the user (click)=setUserSelected(user) . 然后编辑html,以便单击以设置用户(click)=setUserSelected(user)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将对象从一个组件传递到另一个组件 - how can I pass an object from one component to another in angular 如何在Angular 2中将对象从一个组件传递到另一个组件? - How to pass object from one component to another in Angular 2? 如何将json对象的一个​​组件传递给angular中的另一个组件? - How to pass json object one component to another component in angular? 如何以角度从另一个组件传递对象? - How can i pass object one component from another component in angular? 如何在单击按钮时将 id 和对象从一个组件传递到另一个组件的 angular 函数? - How do I pass an id and object on click of a button from one component to another component' s function in angular? Angular 4 - 将对象从一个组件传递到另一个组件(没有父 - 子层次结构) - Angular 4 - Pass an object from one component to another (no parent - child hierarchy) 无法在 angular 2 中将响应 object 从一个组件传递到另一个组件 - Unable to pass response object from one component to another in angular 2 如何将角度2中的数据从一个组件传递到另一个组件? - how to pass data from one component to another component in angular 2? 如何在Angular2中将值从一个组件传递到另一个组件? - How to pass value from one component to another component in Angular2? 如何将对象从一个组件传递到另一个组件以在angular2中建立数据模型? - how to pass an object from one component to another to build a data model in angular2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM