【问题标题】:ngIf and click not working for dynamic html in angular2ngIf 并单击不适用于 angular2 中的动态 html
【发布时间】:2018-09-19 16:54:00
【问题描述】:

ngIf 和 click 不适用于动态 html。

当使用 innerHtml 加载 html 时,ngIf 和 click 事件未加载。

export class FillInBlanksComponent implements OnDestroy, OnInit {    

     question = '';
     editable = true;
     ngOnInit() {    

         question = '<span *ngIf="editable" name="answers['+incr+']" contenteditable="true" (click)="onclick()"> </span>';
     }
     onClick(){
         alert("clicked!!!!");
     }    
}

【问题讨论】:

    标签: javascript angular


    【解决方案1】:

    你调用的函数是(click)="onclick()",但你已经定义了onClick()

    另一件事是,您可能想要对字符串进行 DOM 清理,否则您会收到警告。请查看this answer 了解如何执行此操作。

    【讨论】:

      【解决方案2】:

      检查你有没有拼写错误使用 (click)="onClick()"

      您在函数调用中使用了小 c

      【讨论】:

        猜你喜欢
        • 2017-02-22
        • 1970-01-01
        • 2016-08-03
        • 2017-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多