通过单击角度2为* ngFor中的另一个组件指定变量值

时间:2017-06-29 14:46:43

标签: angular input selector ngfor pass-data

我在* ngFor中尝试将对象传递给另一个组件,但是只传递了表中的最后一个对象,但是应该显示单击的对象,如何解决这个问题?

  <tr data-toggle="control-sidebar" *ngFor="let user of pagedItems">
      <td><control-sidebar [user]="user">
  </control-sidebar>{{ user.name }}</td>

并在另一个只显示最后一个表行的组件视图中。

1 个答案:

答案 0 :(得分:0)

public ActionResult View(Guid projectID) { OnboardModel model = context.onboard_projectInfos.Where(x => x.projectID == projectID).Select(x => new OnboardModel() { propertymanagername = x.propertymanagername, propertymanagercontactemail = x.propertymanagercontactemail, date_modified = (DateTime)x.date_modified, projectmanagercontactnumber = x.projectmanagercontactnumber, Developer = x.onboard_projectCreate.Developer, status1 = x.onboard_projectCreate.status1, ProjectName = x.onboard_projectCreate.ProjectName }).SingleOrDefault(); var pix = projectID.ToString(); context.onboard_BuildingInfos.Where(x => x.buildprojectID == pix).GroupBy(x => x.buildprojectID).Select(g => { model.totalres = g.Sum(b => b.numberofres); model.totalcom = g.Sum(b => b.numberofcommer); }); return View(model); } 数据绑定到user

control-sidebar,问题出在这里。

修改代码:

[user]="userDtails"

确保您已在组件类中定义了<tr data-toggle="control-sidebar" *ngFor="let user of pagedItems"> <td (click)="getUser(user)"><control-sidebar [user]="user"> </control-sidebar>{{ user.name }}</td> 方法。

请注意,我已设置:getUser。您希望将[user]="user"对象传递到user

现在一切都应该正常工作。

编辑

由于您要显示单击的control-sidebar,您可以将方法绑定到user处理程序并传递(click)对象。例如:

user