2017-01-12 8 views
-1

私は角度2のMVCを使用しています。ViewBag値またはハードコード値を入力としてapp.component.tsに渡そうとすると、null値が表示されます。私も角度2ルートコンポーネントにViewBagを渡す

index.cshtml

@{ 
    ViewData["Title"] = "Home Page"; 
} 
<div> 

    <pm-app [linkID][email protected]></pm-app> 

</div> 

app.component.ts

運とElementRefを使用してみました
import { Component, OnInit,Input,ElementRef} from '@angular/core'; 
import { NavMenuService } from './navmenu/navMenu.service'; 
//import { ITitle } from './app'; 

@Component({ 
    selector: 'pm-app', 
    moduleId: module.id, 
    templateUrl: 'app.component.html', 
    providers: [NavMenuService] 
}) 
export class AppComponent implements OnInit { 
    @Input() linkID: any; 
    pageTitle: any[]; 
    title: string; 
    titleID: number; 
    errorMessage: string; 
    constructor(private _navMenuService: NavMenuService, elm: ElementRef) { 
     this.linkID = elm.nativeElement.getAttribute('linkID'); 
     console.log('linkid cons: ' + this.linkID); 
    } 

    ngOnInit(): void { 
     console.log('linkid: ' + this.linkID); 
     this._navMenuService.getLinkName(this.linkID) 
      .subscribe(
      data => { 
       this.titleID = data.result.LinkID; 
       this.title = data.result.LinkName; 


      }, 
      error => this.errorMessage = <any>error); 

    } 

} 

コンソルで出力

linkid cons: null 
linkid : null 

あなたは何をしています教えてもらえます私は行方不明?

答えて

1

はそれを得た、 はちょうど私のインデックスページ を変更しなければならなかったの代わりに、[のlinkID] = @ ViewBag.GWLinkID

@{ 
    ViewData["Title"] = "Home Page"; 
} 
<div> 

<pm-app [email protected]></pm-app> 

</div> 
関連する問題