简体   繁体   English

如何在 aurelia 中创建自定义 textchanged 事件

[英]how to create customize textchanged event in aurelia

textchanged event Consumer should pass event and not in control.Consumer will excute their code on textchange event. textchanged 事件消费者应该传递事件而不是控制。消费者将在 textchange 事件上执行他们的代码。 Component will just expose that event.组件只会公开该事件。 ??? ??? this is requirement how can I create in aurelia?这是要求我如何在 aurelia 中创建?

component零件

textbox.html文本框.html

<input type="text" class="form-control change.delegate="inputValueChange()">

textbox.ts文本框.ts

constructor() {

  }

  attached() {
    this.controller.validateTrigger = validateTrigger.changeOrBlur;
    this.controller.addRenderer(new BootstrapFormRenderer());
    this.controller.validate();
   } 

    public inputValueChange(newValue,oldValue){
      console.log(newValue)
      }
    }

app.html应用程序.html

<template>
<textbox  maxlength="10" autocomplete="on"></textbox>
<template>

You can use custom events - in your textbox component add this to the constructor:您可以使用自定义事件 - 在您的文本框组件中将其添加到构造函数中:

constructor(private element: Element) {}

and in the inputValueChange add something like this:并在 inputValueChange 中添加如下内容:

let event = new CustomEvent('textchange', { 
        detail: <some-data-you-want-to-send>,
        bubbles: true
    });

this.element.dispatchEvent(event);

You should then be able to use the new textchange event like this:然后,您应该能够像这样使用新的 textchange 事件:

<textbox  maxlength="10" autocomplete="on" textchange.delegate="someFunction()"></textbox>

PS. PS。 in your example you are missing a " after the form-control / before change,delegate, that will probably prevent the inputValueChange from being called at all now在您的示例中,您在表单控件之后/更改之前缺少一个“,委托,这可能会阻止 inputValueChange 现在被调用

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

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