2017-01-12 54 views
-1

我正在使用MVC和angular 2.當我嘗試將ViewBag值或硬編碼值作爲輸入傳遞給我的app.component.ts時,我看到空值。我甚至使用ElementRef沒有運氣Angular 2將ViewBag傳遞給根組件

index.cshtml

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

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

</div> 

app.component.ts

試圖
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> 
相關問題