我有一个代表导航栏 full-layout.component.ts
的组件。导航栏中的项目是从服务器接收的(但出于这个问题的目的,我会硬编码)。
对于每个项目,都有routerLink
与之关联,因此当用户点击任何项目时,会将其定向到名为department.component.ts
的组件。在此组件中,提供了DepartmentService
,它使http get()
请求并返回一个字符串数组。
目前,当我在 full-layout.component.ts
并点击任何项目时,它会完美地指示我并显示正确的数据。但是,当我尝试从那里点击另一个项目时,它没有做任何事情,我不知道为什么。
NB:导航栏中的所有项目都显示服务器中的部门列表。
这是我的代码:
ul包含我的商品
full-layout.component.html
<ul class="nav-dropdown-items">
<li class="nav-item" *ngFor="let item of dropDownItems">
<a #clicked class="nav-link" routerLinkActive="active" [routerLink]="[item.routerLink]" (click)="send(item.name)" >
<i class="icon-puzzle"></i>{{item.name}}
</a>
</li>
</ul>
full-layout.component.ts
@Component({
selector: 'app-dashboard',
templateUrl: './full-layout.component.html',
providers: [SharedService]
})
export class FullLayoutComponent implements OnInit {
service;
constructor(service: SharedService) { this.service = service; }
ngOnInit() {}
send(str) { this.service.saveData(str); }
dropDownItems = [{
routerLink: '/components/departments',
name: 'Artshums'
},
{
routerLink: '/components/departments',
name: 'Dentistry'
},
{
routerLink: '/components/departments',
name: 'Law'
},
{
routerLink: '/components/departments',
name: 'IOPPN'
},
{
routerLink: '/components/departments',
name: 'LSM'
},
{
routerLink: '/components/departments',
name: 'NMS'
},
{
routerLink: '/components/departments',
name: 'Nursing'
},
{
routerLink: '/components/departments',
name: 'SSPP'
},
{
routerLink: '/components/departments',
name: 'Health'
}
];
}
departments.component.ts
@Component({
selector: 'app-departments',
templateUrl: './departments.component.html',
providers: [DepartmentsService]
})
export class DepartmentsComponent implements OnInit {
router; service; myName; _faculty: Faculty;
constructor(service: SharedService, private departmentsService: DepartmentsService) {
this.service = service;
this.myName = service.getData();
}
ngOnInit() {
console.log(this.myName);
this.departmentsService.getData(this.myName).then(
(faculties: Faculty) => this._faculty = faculties
);
}
}
departments.service.ts
@Injectable()
export class DepartmentsService {
constructor(
private http: Http
) {}
getData(facultyName): Promise < any > {
return this.http.get('/admin/faculty/' + facultyName).toPromise().then(response => response.json()).catch(DepartmentsService.handleError);
}
}
departments.component.html
<ul class="departments-box" *ngFor="let fac of _faculty">
<li *ngFor="let dep of fac.Departments">
{{dep}}
</li>
</ul>
如果用户在任何项目中,我该如何实现,它应该显示正确的信息和更新视图?
答案 0 :(得分:0)
您可以在angular2
中使用route参数和ActivatedRoute通过将名称作为路线参数传递来修改 full-layout.html 。
<form id="form"></form>
<input name="someInput" type="text" form="form">
以应用程序部门中的
访问该参数 <a #clicked class="nav-link" routerLinkActive="active" [routerLink]="
[item.routerLink,item.name]">
您的路由网址应修改为
import { ActivatedRoute } from '@angular/router'
departmentName: string = ""; // used for the route parameter.
constructor(private route: ActivatedRoute,.....) {
this.departmentName= route.snapshot.params.name;
}
ngOnInit(){
this.departmentsService.getData(this.myName).then(
(faculties: Faculty) => this._faculty = faculties
);
}