【问题标题】:How to pass input data from template to the component class in ember?如何将输入数据从模板传递到ember中的组件类?
【发布时间】:2020-03-19 00:37:14
【问题描述】:

我正在尝试将输入字段中的数据传递给组件类

这是我的组件类 alpha.js

@tracked choice;

    @action next() {

        console.log(this.choice);
    }

这是我的模板 alpha.hbs

    <Input @value={{this.choice}} type="radio"  />
    <button onclick={{action 'next'}}>Next</button>

到目前为止它是空的。

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: javascript ember.js ember-components


    【解决方案1】:

    &lt;Input 组件设计用于文本输入。对于单选按钮,您需要做一些手动工作。一个简单的方法可能如下所示:

    <input value="one" type="radio" {{on "change" this.change}} />
    <input value="two" type="radio" {{on "change" this.change}} />
    <button onclick={{action 'next'}}>Next</button>
    

    使用此change 操作:

    @action change(event) {
      this.choice = event.target.value;
    }
    

    【讨论】:

    • 谢谢!但只是为了澄清,我知道 this.change 是从组件类调用动作。但是 {{on "change" this.change}} 这里的“change”参数是作为事件传递给组件类的更改操作吗?
    • 这是{{on 修饰符。它基本上调用addEventListener,而changeaddEventListener的第一个参数。所以它会为change 事件触发
    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 2019-12-17
    • 2021-11-14
    • 2022-08-14
    • 1970-01-01
    • 2021-04-21
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多