2017-05-24 22 views
0

私はアングラ2で新しいです、そして、私はニュースのウェブサイトを作成しようとしています。例えば、ホームページには異なるカテゴリの異なる記事が含まれています。 「/スライダー/ 1条」、「/スポーツ/ article3」など。角度2でダイナミックルータリンクを作成する方法は?

私はループにしてみてください。

は、だから私は、JSONの結果は、リンク元が含まれているAPIからのスライダ部で例えば記事を取得しますhtmlの結果はルータリンクが機能しません!ここで

は、サンプルコード

Home.component.html // **そのリンクが '/スポーツ'

<a [routerLink]="[item.link]">{{item.title}}</a> 

Route.config.ts

export const rootRouterConfig: Routes = [ 
    { path: '', redirectTo: 'home', pathMatch: 'full' }, 
    { path: 'home', component: HomeComponent }, 
    { path: 'sport', component: SportComponent } 
]; 

が含まれていると言うことができますです私はエラーを言及することを忘れないでください

Uncaught (in promise): TypeError: Cannot read property 'outlets' of null

私は

*ngIf="mainNewsItem?.length > 0" 

ngif *を使用

import { Component, OnInit } from '@angular/core'; 
import { HomeService } from '../../_services/home.service'; 
import { ContentInfo } from '../../_models/content'; 
@Component({ 
    selector: 'app-home', 
    templateUrl: './home.component.html', 
    styleUrls: ['./home.component.css'], 
    providers: [HomeService] 
}) 
export class HomeComponent implements OnInit { 
    mainNewsItem: ContentInfo[]; 
    constructor(private _homeService: HomeService) { 
    this.mainNewsItem = []; 
    } 
    ngOnInit() { 
     // Get Main News Item 
     this._homeService.getMainNewsItem() 
      .subscribe(content => this.mainNewsItem = content, 
      error => this._homeService = <any>error) 
      ; 
    } 
} 

Home.component.ts divがなくなったが、なぜか? mainNewsItemにデータが含まれていることを意味します。

感謝:)

+0

することができます:あなたのSportComponentあなたはパラメータを受け取るためにあなたのコンストラクタでActivatedRouteを注入することができるで

{ path: 'sport/:articleTitle', component: SportComponent },

Source: Angular doc's: route with parameter

をあなたはプランナーにあなたの問題を再現しますか?コンソールのエラーは何ですか –

+0

[ドキュメントに従って](https://angular.io/docs/ts/latest/guide/router.html#!#route-def-with-parameter)経路パラメータ。理解できない場合は、お気軽にお問い合わせください! – trichetriche

+0

エラー - 未知(約束):TypeError:ヌルのプロパティ 'アウトレット'を読み取れません。 –

答えて

1

あなたはルートパラメーターを使用することができます:constructor(private route: ActivatedRoute)

+0

記事のタイトルがなくても問題は起きますがリンクが '/ sport'のみであっても、HTMLのdomより前にロードされたルートが「Uncaught(約束)」で終わっていると教えてくれました:TypeError: 'アウトレット'のヌルは、それを意味します。 –

+0

ですが、これは次のステップに役立ちます。 –

+0

あなたの「アイテム」はまだロードされていないと思います。これは、親HTML要素に['* ngIf =" "'ディレクティブを追加することで対応できます。](https://angular.io/docs/ts/latest/api/common/index/NgIf-directive.html) –

関連する問題