나는 그 때 나의 필요를 만족시키는 해결책을 찾았다. 저는 WebPack에서 angular-cli를 사용하고 있으며, 이것이 나의 필요에 부합했습니다. "templateUrl : '/ Template/Index'"를 사용하는 모든 예제를 이해하지 못합니다. 여기서 경로는 MVC 뷰의 경로입니다. WebPack이 생성하는 번들 뷰 중 하나에서 경로를 찾을 수 없으므로 간단히 작동하지 않습니다. 어쩌면 그 사람들은 angular-cli와 WebPack을 사용하지 않을 수도 있습니다.
이 stackoverflow 대답 - How can I use/create dynamic template to compile dynamic Component with Angular 2.0? 매우 다음 지시문을 만드는 데 도움이되었습니다. 이 지시문은 mvc 부분 뷰의 출력을 가져 와서 컴파일합니다. 그것은 면도기/서버 논리가 일어나도록 허용하고 일부 각도도 컴파일됩니다. 실제로이 MVC 부분 안에 다른 구성 요소를 포함시키는 것은 문제가있었습니다. 그 일을하게되면, 내가 한 일을 알려주십시오. 필자의 경우에는 서버 렌더링을 수행하고 Angular 2 스파에서 원했던 위치에 정확히 배치해야했습니다.
MvcPartialDirective
import {
Component,
Directive,
NgModule,
Input,
ViewContainerRef,
Compiler,
ComponentFactory,
ModuleWithComponentFactories,
ComponentRef,
ReflectiveInjector, OnInit, OnDestroy
} from '@angular/core';
import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import {Http} from "@angular/http";
import 'rxjs/add/operator/map';
export function createComponentFactory(compiler: Compiler, metadata: Component): Promise<ComponentFactory<any>> {
const cmpClass = class DynamicComponent {};
const decoratedCmp = Component(metadata)(cmpClass);
@NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp] })
class DynamicHtmlModule { }
return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule)
.then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => {
return moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp);
});
}
@Directive({ selector: 'mvc-partial' })
export class MvcPartialDirective implements OnInit, OnDestroy {
html: string = '<p></p>';
@Input() url: string;
cmpRef: ComponentRef<any>;
constructor(private vcRef: ViewContainerRef, private compiler: Compiler, private http: Http) { }
ngOnInit() {
this.http.get(this.url)
.map(res => res.text())
.subscribe(
(html) => {
this.html = html;
if (!html) return;
if(this.cmpRef) {
this.cmpRef.destroy();
}
const compMetadata = new Component({
selector: 'dynamic-html',
template: this.html,
});
createComponentFactory(this.compiler, compMetadata)
.then(factory => {
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
this.cmpRef = this.vcRef.createComponent(factory, 0, injector, []);
});
},
err => console.log(err),
() => console.log('MvcPartial complete')
);
}
ngOnDestroy() {
if(this.cmpRef) {
this.cmpRef.destroy();
}
}
}
의 일부-component.html
@{
ViewBag.Title = "This is some MVC stuff!!!";
}
<div>
<h2>MVC Stuff:</h2>
<h4>@ViewBag.Title</h4>
<h2>Angular Stuff:</h2>
<h4>{{1 + 1}}</h4>
</div>
MvcStuff.cshtml
<mvc-partial [url]="'/stuffs/mvcstuff'"></mvc-partial>
(당신의 MVC 응용 프로그램을 공유하여 스파 도메인을 가정)
in StuffsController.CS
public PartialViewResult MvcStuff() => PartialView();
이것은 올바른 대답입니다. 매력처럼 작동합니다. 서버에서 Angular 템플릿을 미리 렌더링하는 것이 좋다고 생각합니다. 따라서 UserName을 쉽게 렌더링하거나 다른 사용자 별 비즈니스 로직 (권한 등)을 쉽게 수행 할 수 있습니다. –
이것이 유일한 방법입니다. – k11k2
' 'undefined'를 얻는 것이 'ComponentFactory'유형에 할당 할 수 없습니다. '오류 –
k11k2