로그인 한 사용자의 그룹 목록이있는 대시 보드가 있습니다. 이제 사용자가 그룹을 클릭하면 이름, 설명 등의 그룹 기본 정보가 페이지 오른쪽에 인쇄됩니다.이 내용은 다음과 같습니다. 한 구성 요소에서 다른 구성 요소로 변수를 전달하고 두 번째 구성 요소 템플릿 HTML 파일에서 해당 변수를 인쇄하는 방법은 무엇입니까?
HomeComponent는 GroupComponent를라는 하위 구성 요소가 있습니다. 그룹 개체를 HomeComponent에서 GroupComponent로 전달하여 GroupComponent의 이름을 인쇄하려고합니다. 아래는 내가 시도 코드 :
routes.ts
export const routes: Routes = [
{path: '', redirectTo: 'home', pathMatch: 'full'},
{path: 'login', component: LoginComponent},
{
path: 'home', component: HomeComponent, canActivate: [AuthGuard],
children: [
{path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{path: 'group', component: GroupComponent},
{path: 'dashboard', component: DashboardComponent}
]
}
];
home.component.ts
export class HomeComponent implements OnInit {
user: string;
user_id: number;
group_list: string[];
constructor(public router: Router, private _groupService: GroupService) {
// xyz code
}
ngOnInit() {
}
getUserGroups() {
let body: string = '{}';
this._groupService.getAllUserGroups(body).subscribe(
auth => {
let payload = auth.json();
this.group_list = payload.list;
},
error => {
console.log(error);
}
);
}
goToGroupDetail(group) {
console.log(group.id);
let groupObj = new GroupComponent(group.name);
this.router.navigate(['/home/group']);
}
}
home.component.html는
<ul class="nav navbar-nav navbar-right">
<li *ngFor="let group of group_list"><a (click)="goToGroupDetail(group);">{{group.name}}</a></li>
</ul>
group.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'app-group',
templateUrl: './group.component.html',
styleUrls: ['./group.component.css']
})
export class GroupComponent implements OnInit {
group_name:string;
getGroupName(): string {
return this.group_name;
}
setGroupName(value){
this.group_name = value;
}
constructor(name:string) {
this.setGroupName(name);
}
ngOnInit() {
}
}
group.component.html
<p>
group works!
</p>
<div class="col-md-6">
<h2>Name of the Group : {{group_name}}</h2>
</div>
@inputs (고의적 인 말투)에 너무 감사드립니다! – shahakshay94