我正在使用角度为2的MVC。当我尝试将ViewBag值或甚至硬编码值作为输入传递给我的app.component.ts时,我看到空值。我甚至尝试使用ElementRef而没有运气
index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<div>
<pm-app [linkID]=@ViewBag.GWLinkID></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);
}
}
在consol输出
linkid cons: null
linkid : null
你能告诉我我错过了什么吗?
答案 0 :(得分:1)
@{
ViewData["Title"] = "Home Page";
}
<div>
<pm-app linkID=@ViewBag.GWLinkID></pm-app>
</div>