简体   繁体   English

Angular 仅在鼠标移动时绑定

[英]Angular bind only when mouse moves

I have a very weird problem.我有一个非常奇怪的问题。 I've got an input and I want to show a button only when the input has a value.我有一个输入,我只想在输入有值时显示一个按钮。 It's working only if i move the mouse.只有当我移动鼠标时它才有效。

<input class="form-control"
   type="text"
   placeholder="hello there..."
   #helloInput
>

{{helloInput.value}} --> not shows until mouse is moving

<button class="btn btn-success"
   *ngIf="helloInput.value"
>Show me</button>

It's very weird.这很奇怪。 Any idea?任何想法?

Angular updates view only by calling change detection. Angular 仅通过调用更改检测来更新视图。 Most of the time it happens automatically because of Event listeners/XHR/setTimeout/setInterval calls.大多数情况下,由于 Event listeners/XHR/setTimeout/setInterval 调用,它会自动发生。

In your example there's no any event listener known by Angular.在您的示例中,Angular 不知道任何事件侦听器。 Since it's updated by mousemove then there's some listener for it and Angular detected that.因为它是由 mousemove 更新的,所以有一些监听器,Angular 检测到了它。

In order to update your view on any input value change you can simply add empty input event listener:为了更新您对任何输入值更改的视图,您只需添加空input事件侦听器:

<input class="form-control"
   type="text"
   placeholder="hello there..."
   #helloInput
   (input)="0"  <=============== this one
>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM