简体   繁体   English

如何在 Angular 的(鼠标悬停)内分配一个值?

[英]How to assign a value inside a (mouseover) in Angular?

I have want show a value (which it is the return of a function in the component.ts) only when the mouse pointer is over the button.我只想在鼠标指针悬停在按钮上时显示一个值(它是 component.ts 中函数的返回值)。

I want to do something like this.我想做这样的事情。

<div class="dropdown mat-container" style=" margin-left: 2%;">
    <button class="dropbtn" (mouseover)= "myVariable = haveRoutes(template._id)">Tipo</button>
    <div class="dropdown-content btn btn-primary">
        <a>{{myVariable}}</a>
    </div>
</div>

The function in the component.ts component.ts 中的函数

haveRoutes(id){
    this._ApprovalsService.getQuantityByTemplate(id).subscribe(
      (quantity:any) => {
        if(quantity.count == 0){
          return  "Draft"
        }else{
          return "Not draft"
        }
      }
    );
  }

One solution for now, is store the value in the model and change it in the component.ts like this:目前的一种解决方案是将值存储在模型中并在 component.ts 中像这样更改它:

export class GetComponent implements OnInit {
     message = "";
     
     //some other functions

     haveRoutes(id){
        this._ApprovalsService.getQuantityByTemplate(id).subscribe(
          (quantity:any) => {
            if(quantity.count == 0){
              this.message = "Draft"
            }else{
              this.message = "Not Draft"
            }
          } 
      );
  }

And in the html.并在 html 中。

<div class="dropdown mat-container" style=" margin-left: 2%;margin-right: 4%">
    <button class="dropbtn" (mouseover)= "haveRoutes(template._id)">Tipo</button>
        <div class="dropdown-content btn btn-primary">
            <a>{{message}}</a>
        </div>
</div>

It works but it's slower, so I want to try like the first way hopping to get the value quickly.它有效,但速度较慢,所以我想尝试像第一种跳跃方式一样快速获取价值。

You could consider PRE-storing the values in the component, something like this pseudo-code:您可以考虑将值预先存储在组件中,类似于以下伪代码:

component:成分:

quantitiesDictionary:Dictionary = new Dictionary();
quantitiesDictionary.push(id, this._ApprovalsService.getQuantityByTemplate(id));

template:模板:

{{ quantitiesDictionary[id].count == 0 ? 'Draft' : 'Not Draft' }}

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

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