数据绑定ng2组件的模板仅设置为OnInit

乔治·爱德华兹

我有一个angular 2(RC5)组件,该组件进行HTTP调用并将结果设置为该组件的模板。我想将一个值注入到HTTP调用返回的HTML中。因此,例如,返回的HTML中的一行是:

<a class="d2h-file-name" href="{{chapterURL}}">app/views/login/login.xml</a>

但是,这将完全按原样呈现,而无需注入ChapterURL。大概是因为在初始化过程中未设置模板吗?如果是这样,我应该如何将这些动态值注入模板?

这是组件。

@Component({
    selector: 'codestep',
    template: `<div class="codestep" [innerHTML]="content"></div>`
})
export class codeStepComponent {
    @Input() step: string;
    private content: string = '';
    private chapterURL;

    constructor(private route: ActivatedRoute, private http: Http) { }

    ngOnInit() {
        this.chapterURL = './diff/' + this.step + '.html';
        this.getChapter()
            .subscribe(
            chapterContent => this.content = chapterContent,
            error => this.errorMessage = <any>error);
    }

    getChapter(): Observable<any> {
        return this.http.get(this.chapterURL)
            .map(this.extractData)
            .catch(this.handleError);
    }
    private extractData(res: Res) {
        let body = res._body;
        return body;
    }
    //Error handling function here...
}

编辑:

我将http调用返回的源html文件更改为:

<a class="d2h-file-name" href={{chapterURL}}>app/views/login/login.xml</a>

然后将组件的模板更改为:

template: `<div class="codestep" [innerHTML]="content|rawHtml"></div>`

在哪里rawHtmlbypassSecurityTrustHtml()函数清除内容的管道在哪里DomSanitizationService但是,我仍然得到相同的结果,呈现的结果是:

<a class="d2h-file-name" href="gitURL">app/views/login/login.xml</a>

如果我对ng.probe($0)在浏览器中选择的组件进行操作,则返回的结果对象具有属性,但列出的唯一属性是innerHTML,其他都没有...

萧敬腾

2方法

方法1-搜索并替换

如果在初始化期间只需要更新一次数据,则这很简单。

ngOnInit() {
    this.chapterURL = './diff/' + this.step + '.html';
    this.getChapter()
        .subscribe(
        chapterContent:string => {

            // Pre-process the content
            processedContent = chapterContent.replace('{{chapterURL}}',this.chapterURL);

            this.content = processedContent;
        },
        error => this.errorMessage = <any>error);
}

方法2-动态组件

Angular 2不支持组件模板运行时更新。

innerHTML不会满足您的要求,因为Angular2不会解析其内容。因此,innerHTML中的数据绑定将不起作用。

为了归档运行时模板更新,或更确切地说,归档运行时模板是使用动态组件。

RadimKöhler在此处提供示例的详细答案:https : //stackoverflow.com/a/38888009/1810391

http://plnkr.co/edit/iXckLz?p=preview

以下是我整理的一个非常简单的示例:

cf.com.ts

import { Component, ComponentRef, ViewChild, ViewContainerRef } from '@angular/core';

import { RuntimeCompiler } from '@angular/compiler';

import { CfModule } from './cf.module';

@Component({
    selector: 'cf-com',
    template: `
        <h1>{{title}}</h1>
        <button (click)="template1()">Template 1</button>
        <button (click)="template2()">Template 2</button>
        <button (click)="moreChild()">More Child</button>
        <template [ngIf]="childRef" #child></template>`
})
export class CfCom {
    title = 'Component Factory Test';

    // reference for html element with #child tag
    @ViewChild('child', { read: ViewContainerRef }) protected childComTarget: ViewContainerRef;
    // Child component reference
    protected childRef: ComponentRef<any>;

    constructor(private compiler: RuntimeCompiler) { }

    // Child Input. Use object, not basic type
    childInput = { counter: 0 };

    // Click to get more children
    moreChild() {
        this.childInput.counter++;
    }

    // Click to use template 1
    template1() {
        let t = 'Child:{{j.counter}}';
        this.createChild(t);
    }

    // Click to use template 1
    template2() {
        let t = 'Children:{{j.counter}}';
        this.createChild(t);
    }

    createChild(t: string) {
        // Destroy child if exist
        if (this.childRef) {
            this.childRef.destroy();
            this.childRef = null;
        }

        // cf-child class
        @Component({
            selector: 'cf-child',
            template: t // template from parameter t
        })
        class CfChildCom {
            j; // will be bind with parent childInput, see below
        }

        this.compiler.compileComponentAsync<any>(CfChildCom, CfModule)
            .then(factory => {
                this.childRef = this.childComTarget.createComponent(factory, 0);

                // This is how parent variable bind with child variable
                this.childRef.instance.j = this.childInput;
            });
    }
}

cf.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { COMPILER_PROVIDERS } from '@angular/compiler';

import { CfCom } from './cf.com';

@NgModule({
    imports: [BrowserModule],
    exports: [CfCom],
    providers: [COMPILER_PROVIDERS],
    declarations: [CfCom]
})
export class CfModule { }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 2组件构造函数与OnInit

Angular2组件@Input双向绑定

Angular 2组件-访问DOM(或仅从JS创建不带模板的组件)

Angular2组件模板与视图模板

Angular2组件是单向绑定还是输入?

如果我没有在Activity onDestroy()中将Dagger 2组件设置为null,会发生什么?

在jQuery插件模板内绑定Angular2组件

在Angular 2组件模板中嵌入小部件

如何获取Angular 2组件的html模板?

通过angular1 ui-router导航后,ng2组件中的更改检测无法正常工作

Angular 2组件DOM在OnInit方法中解析bean之前已绑定到组件中的属性

如何在html表和angular 2组件之间实现2路数据绑定

Angular ng2文件上传问题

AngularJS 1.5组件将数据绑定到控制器并使用模板url查看

UI-路由器混合:在触发具有NG2组件的NG1状态之前,NG2状态不起作用

如何与ng2文件一起提交输入数据上传Angular ION

ng2图表中条形图的数据集设置不正确

Joomla 2.5组件:如何在管理表单中为媒体字段设置目录?

具有旧版ng1组件依赖项的ng2 AsyncRoute

我在本机脚本中跨ng2组件管理UI引用

如何使angular2组件ng无效

Angular 2组件中未显示JSON数据

如何在Angular 2中使用ng2文件上传提交帖子数据?

将ng2组件模板设置为变量?

Angular2组件未将属性绑定到DOM

删除Angular 2组件的重复模板标记

ng2 从组件继承并扩展父模板

ng2 将表单组作为参数发送到组件

React:将同一组件的多个映射实例设置为单个组件