Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ember.js中设置文本框值_Ember.js - Fatal编程技术网

如何在Ember.js中设置文本框值

如何在Ember.js中设置文本框值,ember.js,Ember.js,如果这个问题太傻,请原谅。我自己正在研究Ember.js,我遇到了一个问题,我需要动态设置文本框的值,我不知道怎么做,现在我使用jquery通过使用$(“#results”)[0]来克服这个错误我知道这不是正确的方法,我怎么能在Ember,js中做同样的事情呢 我目前的代码如下 App = Em.Application.create(); App.BaseCurrency = Em.Object.extend({ id: null, name: null }); App.Dest

如果这个问题太傻,请原谅。我自己正在研究Ember.js,我遇到了一个问题,我需要动态设置文本框的值,我不知道怎么做,现在我使用jquery通过使用
$(“#results”)[0]来克服这个错误我知道这不是正确的方法,我怎么能在Ember,js中做同样的事情呢

我目前的代码如下

App = Em.Application.create();
App.BaseCurrency = Em.Object.extend({
    id: null,
    name: null
});
App.DestCurrency = Em.Object.extend({
    id: null,
    name: null
});
App.BaseSelector = Em.ArrayController.extend({
    content: [
        App.BaseCurrency.create({ id: "INR", name:" Indian Rupee (INR)" }),
        App.BaseCurrency.create({ id: "USD", name: "US Dollar (USD)" })
    ]
});
App.baseSelector = App.BaseSelector.create();

App.DestSelector = Em.ArrayController.extend({
    content: [
        App.DestCurrency.create({ id: "INR", name: " Indian Rupee (INR)" }),
        App.DestCurrency.create({ id: "USD", name: "US Dollar (USD)" })
    ]
});
App.destSelector = App.DestSelector.create();

App.ConvertCurrency = Em.ObjectController.extend({
    content: [],
    amount:0,
    baseCur: null,
    destcur: null,
    result: 0,
    convert: function () {
        var amount = this.get('amount');
        var from = this.get('baseCur');
        var to = this.get('destCur');
        $.ajax({
            type: "POST",
            url: "Home/ConvertCurenncy",
            data: "{amount:" + amount + ",from:'" + from + "',to:'" + to + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#results")[0].value = data;
            }
        });
    }
});

App.convertCurrency = App.ConvertCurrency.create();
和html

<script type="text/x-handlebars">
    AMOUNT : {{view Ember.TextField valueBinding="App.convertCurrency.amount"}}
    BASE : {{view Ember.Select contentBinding="App.baseSelector" optionLabelPath="content.name" optionValuePath="content.id" valueBinding="App.convertCurrency.baseCur" }}
    DEST : {{view Ember.Select contentBinding="App.destSelector" optionLabelPath="content.name" optionValuePath="content.id" valueBinding="App.convertCurrency.destCur" }}
    <button id="btnConvert"{{action "convert" target="App.convertCurrency"}}>convert</button>
    RESULT :<input type="text" readonly=true id="results" />
</script>

金额:{{view Ember.TextField valueBinding=“App.convertCurrency.AMOUNT”}
BASE:{{view Ember.Select contentBinding=“App.baseSelector”optionLabelPath=“content.name”optionValuePath=“content.id”valueBinding=“App.convertCurrency.baseCur”}
DEST:{{view Ember.Select contentBinding=“App.destSelector”optionLabelPath=“content.name”optionValuePath=“content.id”valueBinding=“App.convertCurrency.destCur”}
转换
结果:

当您几乎到达目的地时,您可能要做的是自定义一个
Ember.Texfield
,例如:

App.ResultField = Ember.TextField.extend({
  type: 'text',
  attributeBindings: ['readonly'],
  readonly: true
})
然后像这样使用它:

Result: {{view App.ResultField valueBinding="App.convertCurrency.result"}}
然后在
success
回调中,将
App.convertCurrency.result
设置为接收的值:

...
success: function (data) {
  App.convertCurrency.set('result', data);
}
...
绑定将负责更新textfield的值


希望有帮助。

您能在JSFIDLE或jsbin中显示一些代码,看看您的设置是什么样子吗?@直觉像素请查看更新的问题您可能已经知道这一点,但Ember.TextFields默认为键入“text”。是的,谢谢您的提示,我知道,但我想更详细一些,以帮助理解它可以明确定义:)