在Angular 2中将String转换为HTML代码

时间:2017-01-02 08:49:01

标签: html function angular

将字符串转换为HTML代码时遇到问题。我只想将&#160转换为HTML代码,这个值来自变量String。这是我的代码

ngOnInit(){
    this.submitButtonText = 'Save';
    this.spaceMenu= '   ';
    this.form = this.fb.group({
        'id': [],
        'name': ['', Validators.compose([Validators.required,Validators.minLength(3)])],
        'description': []
    });
    this.name = this.form.controls['name'];
    this.description = this.form.controls['description'];
    this.service.getMenuGroup()
        .subscribe(
            data => this.setMenuGroup(data['menuGroup']),
            err => this.logError(err),
            () => this.finishedGetIndexList('Save')
        );
        this.statusForm = 'Add';
        if(this.id){
            this.fetchData();
        }
    }
<div *ngFor="let item of menuList" [ngClass]="class">
    <label class="checkbox-inline custom-checkbox nowrap">
        <md-checkbox
        [name]="checkboxMenu"
        [checked]="(_checkingList(item,selectedMenu))"
        [indeterminate]="(_checkIsIndeterminate(item,selectedMenu))"
                                            (change)="_onChange(item,$event)"
        [disabled]="true">
        {{item.name}}
        </md-checkbox>
    </label>
</div>

this.spaceMenu,它是一个包含值&#160的字符串变量。我想在<md-checkbox.

之前插入它

如果有人可以帮助我?

1 个答案:

答案 0 :(得分:0)

<label ...>
<span [outerHTML]="spaceMenu | safeHtml"></span>
<md-checkbox ...

有关safeHtml管道的详细信息,请参阅In RC.1 some styles can't be added using binding syntax
不确定这里是否有必要。