2017-03-27 1 views
0

는 나는이 링크 How to use variable to define templateUrl in Angular2 따라했습니다,하지만 난 오류가 점점 오전 :template2를 angular2에서 varible로 전달하는 방법은 무엇입니까?

ERROR Error: Cannot find module "." at webpackMissingModule (html-outlet.ts:26) [angular] at HtmlOutlet.ngOnChanges (html-outlet.ts:26) [angular] at checkAndUpdateDirectiveInline (core.es5.js:10756) [angular] at checkAndUpdateNodeInline (core.es5.js:12137) [angular] at checkAndUpdateNode (core.es5.js:12105) [angular] at debugCheckAndUpdateNode (core.es5.js:12734) [angular] at debugCheckDirectivesFn (core.es5.js:12675) [angular] at Object.eval [as updateDirectives] (StaticContentComponent.html:3) [angular] at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:12660) [angular] at checkAndUpdateView (core.es5.js:12072) [angular]
at callViewAction (core.es5.js:12387) [angular] at execCompon enter code here entViewsAction (core.es5.js:12333) [angular] at checkAndUpdateView (core.es5.js:12078) [angular]
at callViewAction (core.es5.js:12387) [angular]

내 footer.html를 :

<div><a [routerLink]="['/aboutus']"> About</a> </div> 

routing.ts 

import { ModuleWithProviders } from '@angular/core';`enter code here` 
import { Routes, RouterModule } from '@angular/router'; 
import { StaticContentComponent } from '../common/footer/staticcontent.component'; 

const routes: Routes = [ 
    { path: 'aboutus', component: StaticContentComponent } 
    ]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(routes); 



staticcontent.component.ts 

import { Component } from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 

import { ArticleService } from './article.service'; 
import { HtmlOutlet } from './html-outlet' 
class Article { 
    title: string; 
    html: string; 
} 

@Component({ 
    //selector: 'static_content', 
    template: ` 
    <div> 
     <html-outlet [html]="article?.html"></html-outlet> 
    </div> 
    ` 
}) 
export class StaticContentComponent{ 
    article: Article; 
    constructor(private articleService: ArticleService) { 
    alert(); 
    this.articleService.getArticle().subscribe(data => { 
     alert('get'); 
     this.article = data; 
     alert(data.html) 
     console.log(data); 
    }) 
    } 

    ngOnInit(): void { 

    } 

} 


html-outlet.ts 

import { 
    Component, 
    Directive, 
    NgModule, 
    Input, 
    ViewContainerRef, 
    Compiler 
} from '@angular/core'; 

import { CommonModule } from '@angular/common'; 

@Directive({ 
    selector: 'html-outlet' 
}) 
export class HtmlOutlet { 
    @Input() html: string; 

    constructor(private vcRef: ViewContainerRef, private compiler: Compiler) {alert('htl'+this.html);} 

    ngOnChanges() { 
    const html = this.html; 
    if (!html) return; 

    @Component({ 
     selector: 'dynamic-comp', 
     templateUrl: html 
    }) 
    class DynamicHtmlComponent { }; 

    @NgModule({ 
     imports: [CommonModule], 
     declarations: [DynamicHtmlComponent] 
    }) 
    class DynamicHtmlModule {} 

    this.compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule) 
     .then(factory => { 
     const compFactory = factory.componentFactories.find(x => x.componentType === DynamicHtmlComponent); 
     const cmpRef = this.vcRef.createComponent(compFactory, 0); 
     }); 
    } 
} 

Please help me... 
+0

질문이있는 코드를 추가하십시오. –

+0

@Bilal, 내 질문을 편집했습니다. – latha

답변

0

staticcontent.component.ts

<html-outlet [html]="article?.html"></html-outlet> 

기사를? 이 코드의 html은 유효한 templateUrl이 아닌 것 같습니다. "article.html"이어야하는지 확인하십시오.

+0

article.json 파일을 사용하고 해당 json 파일에서 데이터를 가져 왔습니다. artcle.json { "제목": "내 첫 번째 기사", "html": "app/articles/first.html"} 그래서 기사? .html 제공 "app/articles/first.html" – latha

+0

대체합니까? "article? .html"with "app/articles/first.html"work? –

관련 문제