angular - 如何在angular2的一个组件中有多个 View

标签 angular typescript angular2-routing angular2-services angular2-components

我有一个代表导航栏的组件 full-layout.component.ts。导航栏中的项目是从服务器接收的(但出于这个问题的目的,我将进行硬编码)。

对于每个项目都有一个 routerLink 与之关联,因此当用户点击任何项目时,它会被定向到一个名为 department.component.ts 的组件。在此组件中,提供了一个 DepartmentService,它发出 http get() 请求并返回一个字符串数组。

目前,当我在 full-layout.component.ts 中并点击任何项目时,它会完美地将我引导到那里并显示正确的数据。但是,当我尝试从那里单击另一个项目时,它什么也没做,我不确定为什么。

注意:导航栏中的所有项目均显示来自服务器的部门列表。

这是我的代码:

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>

问题

当用户在任何项目中时,我如何才能显示正确的信息并更新 View ?

最佳答案

在angular2中可以使用route参数和ActivatedRoute

通过将名称作为路由参数传递来修改您的 full-layout.html

 <a #clicked class="nav-link" routerLinkActive="active" [routerLink]="
           [item.routerLink,item.name]"> 

访问您的app-departments中的参数

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
    );
}

你的路由url应该修改为

/components/departments/:name

关于angular - 如何在angular2的一个组件中有多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321781/

相关文章:

javascript - 如何为以下 Angular 方法编写测试用例

Angular:同时使用 ViewChild 和输入、副作用还是最佳实践?

angular2自定义表单输入无法读取未定义的属性 'name'

javascript - 通过组件传递 ng-template

Angular 2 路由器链接激活,两个链接激活

javascript - 实现 Angular Guard 的重要性

angular - 我可以使用 Angular i18n 来翻译 typescript 代码中的字符串文字吗

javascript - 尝试通过 URL 访问时,Angular 2 动态路由不起作用

html - 避免在 html 卡中显示大数字

typescript - 如何使用angular2中的链接参数数组从子组件导航到上层路由?