html - 在 Typescript Angular 2 中设置 HTML 空格代码

标签 html angular

我只是一个关于如何在 html 中将空间设置为我的值的问题。 我有代码:

    setValuetoComboBox(test:string, lvl:number){
    let lastTest:string;
    let spaceString:string;
    // 
    spaceString='&#160';
    for(var i = 1; i <= lvl; i++){
        spaceString=spaceString + '&#160';
    }
    lastTest = spaceString + test;
    return lastTest;
}

在这段代码中,我有   来在 html 中腾出空间。我有 html 代码:

                                    <select [formControl]="parent" class="form-control">
                                    <option value="-1">Root</option>
                                    <option value="{{test.id}}" *ngFor="let test of parentValue">
                                        {{setValuetoComboBox(test.name, test.lvl)}}
                                    </option>
                                </select>

结果是:

The Result

这是错误的结果,我想要空间而不是代码 。有人能找到我的问题的答案吗?

最佳答案

我猜你真正想要的是

<option value="{{test.id}}" *ngFor="let test of parentValue"
    [innerHTML]="setValuetoComboBox(test.name, test.lvl)">
</option>

使用 {{ }} 将插入文字字符串。

您可能需要使用 sanitizer 将其标记为安全,例如How to bind raw html in Angular2中所示

您应该意识到 setValuetoComboBox(...) 可能会被频繁调用(每次 Angular 运行更改检测时)。通常最好将调用结果分配到属性中并绑定(bind)到该属性,而不是绑定(bind)到函数。

因此,通常不鼓励绑定(bind)到 View 中的函数。它可能会导致严重的性能问题。

关于html - 在 Typescript Angular 2 中设置 HTML 空格代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41256814/

相关文章:

typescript - 使用 JSPM 和 Typescript 改进 Webstorm 11 中的导入

html - 如何在 Angular 输入中添加垫选择

javascript - Angular 2 ChangeDetectionStrategy.OnPush 在输入不改变时触发 ngAfterViewChecked

html - 插入背景横幅

javascript - 如何将此示例应用到我已有的代码行?

PHP - 文件上传 - 内部发生了什么?

javascript - 使用 *ngFor 将 Angular Material 中的对象数组分组

php - 如何中心对齐来自数据库的 'n' 图像数量

javascript - 单击文本或图像时如何显示 HTML 输入日期?

angular - *ngFor对象比较使用 '==='