小编Mel*_*msy的帖子

Angular 性能:DOM 事件导致不必要的函数调用

我有一个带有 DOM 事件(输入)的输入元素的简单页面。HTML 页面还调用一个函数,该函数仅通过 console.log 输出一些内容。现在,当我打开页面时,它会显示日志,但是当我在输入字段中输入内容时,它也会在每次输入内容时触发该功能..(实际上,当我输入一个字母时,它会在 console.logs每次两次)

为什么会发生这种情况?如何预防?我阅读了一些关于 的内容changeDetection,但还有其他解决方案吗?

HTML:

{{test()}}

<input class="input-msg" [value]="textValue" (input)="textValue = $event.target.value;">
Run Code Online (Sandbox Code Playgroud)

.ts:

export class TestComponent implements OnInit {

  constructor() { 
  }

  test() {
    console.log('test message');
  }
}
Run Code Online (Sandbox Code Playgroud)

预期行为:

{{test()}}输入的东西到输入字段时,不应该叫

html performance angular

2
推荐指数
1
解决办法
1218
查看次数

标签 统计

angular ×1

html ×1

performance ×1