简体   繁体   English

将参数绑定到余烬组件并从控制器处理它

[英]Binding a parameter to an ember component and handle it from the controller

I want to create the following component in ember (templates / components / echo-hlabel-tf.hbs) 我想在余烬中创建以下组件(模板/组件/ echo-hlabel-tf.hbs)

<div id="echo-hlabel-tf">
  <span id="tf-label">{{label}}</span>
  <span id="tf-value">
    <input type="text" value={{textValue}}
    {{action "validateRegExp" textValue regExp post on="focusOut"}} 
    {{action "showText" textValue post on="mouseUp"}} 
    />
  </span>
</div>

Just a textfield and a label placed in the same row . 只是在同一行中放置了一个文本字段和一个标签。 I have created the functions in the controller of the component (components / echo-hlabel-tf.js): 我已经在组件的控制器(components / echo-hlabel-tf.js)中创建了函数:

import Ember from 'ember';

export default Ember.Component.extend({
actions: {
    validateRegExp(text,regExpStr){
        let regExpObj = new RegExp(regExpStr)
        if(regExpObj.test(text)){
            console.log('entry matches')
        }else{
            console.log('entry is wrong');  
        }

    },
    showText(text){
        console.log(text);
    }
}
});

I want to use this component from another template (template / programmers.hbs) : 我想从另一个模板(template / developers.hbs)使用此组件:

<ul>
 {{people-list title="List of programmers" people=model}}
</ul> 
<div>
 {{echo-hlabel-tf 
    label="Textfield horizontal"
    textValue="..." 
    regExp="^([a-z0-9]{5})$"
 }}
</div>

The thing is that , even the actions are fired as the events trigger, the variable that represents the value of the input (textValue) always stands the same ("...") . 事实是,即使动作是在事件触发时触发的,代表输入值(textValue)的变量也始终保持相同(“ ...”)。 seems that the binding between the outer template, and the components is created at the beginning, but if I put more letters in the textfield , the value of textValue remains the same and doesn't change . 似乎外部模板和组件之间的绑定是在开始时创建的,但是如果我在文本字段中放置更多字母,则textValue的值将保持不变并且不会改变。 I would like to, as I'm adding letters to the textBox, print them with the console with the method showText, but I always print "..." 我想在文本框中添加字母时,使用showText方法在控制台上打印它们,但是我总是打印“ ...”

You are using pure html5 input that does not do two way binding by itself. 您使用的是纯html5 input ,它本身不进行双向绑定。 That is even if you type something, it is not reflected to textValue attribute. 即使您键入某些内容,它也不会反映到textValue属性中。 You can use ember's builtin input component as in this twiddle . 您可以像在这个twiddle中那样使用ember的内置input组件。 I believe this will solve your problem. 我相信这会解决您的问题。

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

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