角度2加载组件太早

时间:2016-05-19 08:00:37

标签: javascript typescript angular typescript1.8

我有一个角度2,它有点按照以下方式构建

app.component - 有几条路线 - 其中一条路线是some-data-component,它不是首先显示的默认路线。

some-data-component - 有一个列表,您可以在其中选择一个项目 - 当它被选中时,我想在数据详细信息组件中显示所选项目。

问题是,在应用程序开始时加载了data-detail-component - 我从它的html中得到以下错误

  

无法阅读财产'学生'未定义的

这是有道理的,因为我还没有选择一个项目,所以它应该是未定义的

有没有办法构建我的应用程序,以便除非显示它们,否则不会构建其他组件?

这是我的代码:

app.component

import { Component, OnInit } from '@angular/core';
import {Routes, Router, ROUTER_DIRECTIVES} from '@angular/router';

import { Dashboard } from './dashboard/dashboard.component';
import {SomeDataComponent} from './some-data-component/some-data-component';

@Component({
    selector: 'my-app',
    templateUrl: './app/components/app.component.html'
  , directives: [ROUTER_DIRECTIVES] 
})
@Routes([
        { path: '/somedata', component: SomeDataComponent},
        { path: '/dashboard', component: Dashboard }
])
export class AppComponent implements OnInit {

    mobileView: number = 992;
    toggle: boolean = false;

    constructor(private router: Router) {
        this.attachEvents();
    }

    ngOnInit() {
        this.router.navigate(['/dashboard']);
    }
}

一些数据组分

import {SomeDataListView} from '../some-data-list-view/some-data-list-view';
import {DataDetailComponent} from '../data-detail/data-detail.component';

import {SomeService} from '../../services/some_service';    

@Component({
    selector: 'some-data-component',
    providers: [SomeService],
    templateUrl: 'app/components/some-data/some-data.html',
    directives: [SomeDataListView, DataDetailComponent]
})
export class RoutesComponent {
    data;
    selectedData;

    constructor(private someService: SomeService) {
        this.data=[]
    }

    ngOnInit() {
        this.data= this.SomeService.all();
    }

    selectedRouteChanged(route) {
        this.selectedRoute = route;
    }
}

一些-data.html



<rd-widget>
    <rd-widget-body classes="medium no-padding">
        <div>
            <img src="{{route.imgSrc}}" />
            <label>Neighborhood: {{route.name}}</label>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th class="text-center">ID</th>
                            <th>Neighborhood</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr *ngFor="let student of route.students" (click)="setSelected(route)">
                            <td>{{student.id}}</td>
                            <td>{{student.neighborhood}}</td>
                            <td>
                                <span class="{{route.tooltipcls}}">
                                    <i class="fa {{route.icon}}"></i>
                                </span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </rd-widget-body>
</rd-widget>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以使用Elvis运算符:

<tr *ngFor="let student of route?.students" ...