2017-01-12 108 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> 
相关问题