将Razor模型传递给Angular 5组件 [英] Pass Razor model to Angular 5 component
问题描述
我在asp.net剃须刀视图中具有此功能:
I have this in my asp.net razor view:
@model IEnumerable<MyClass>
<app-root [(myModel)]="'@Model'"></app-root>
还有这个
import { Component } from '@angular/core';
import { Input } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styles: []
})
export class AppComponent {
title = 'app';
@Input()
public myModel: any;
ngOnInit() {
console.log(this.myModel);
}
}
控制台中的输出显示"udefined" ... 我试图将'myModel'命名为ngModel,我试图绑定[]而不是[()],但是没有运气.
Output in console says "udefined"... I have tried to name 'myModel' as ngModel, i have tried to bind with [] instead [()], but no luck.
如何将Razor模型传递给Angular 5组件?
How can i pass Razor model to Angular 5 component?
推荐答案
我遇到了与您相同的问题,未定义,最后这对我有用.如果您找到了更好的方法,请分享.
I faced the same issue as you, undefined, finally this worked for me. If you have found a better way please share.
cshtml:
< input type ='hidden'id ="radhe" value ="@ ViewBag.ClubID"/>
< book-slot></book-slot>
cshtml:
<input type='hidden' id="radhe" value="@ViewBag.ClubID"/>
<book-slot></book-slot>
组件:
import { Component, OnInit, Inject, Injectable} from '@angular/core';
import { DataService } from '../Shared/DataService';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'book-slot',
templateUrl: './book-slot.component.html',
styleUrls: ['./book-slot.component.css']
})
@Injectable()
export class BookSlotComponent implements OnInit {
clubId: number;
constructor(private data: DataService, @Inject(DOCUMENT) private document: any) {
this.clubId = this.document.getElementById("radhe").value;
}
}
这篇关于将Razor模型传递给Angular 5组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!