如何在不使用角度$ compile的情况下将打字稿中的对象传递给HTML字符串

时间:2019-01-30 13:32:52

标签: html angularjs typescript

这是一个有角度的程序,但我们编写的程序包使用打字稿。基本上,我想解决的是如何使“ this”作为真正的角度绑定对象进入HTML字符串。我不能使用$ compile,因为这是一个打字稿文件。任何帮助表示赞赏。目前,我只能将对象片段作为字符串传递,例如this.name。

    export class GroupView extends ObjView {
        name: string = "GroupView";

        constructor(parentView: GroupView, protected group: Group) {
            super(parentView, group)
            if(group){
                this.name = group.name
                this.structureItem = '<custom-directive this-model="' + this + '" name="' + this.name + '"></custom-directive>';
            }
        }
   }

将this.structureItem添加到页面后,我需要能够通过引用访问此模型...实际对象不是它的副本。

1 个答案:

答案 0 :(得分:1)

我建议另一种方法。使用ng-if - 指令在HTML模板。例如:

TS

constructor(parentView: GroupView, protected group: Group) {
    super(parentView, group)
        if(group){
            this.name = group.name;
    }
}

HTML

<custom-directive ng-if="group" name="name"></custom-directive>

此方式,元件才可见当group是有效的。