【问题标题】:Ember checkbox and valueEmber 复选框和值
【发布时间】:2014-04-02 16:49:28
【问题描述】:

我在循环中创建了复选框。我将选中的设置为属性名称,并希望在单击时从复选框中获得动态值。在属性(键,值)中,我得到了错误的东西-我的属性的标签而不是值。 Ember 中是否有一种简单的方法可以从复选框中获取值? 任何帮助深表感谢。 在 HTML 中:

 {{#each url in controllers.application.env.urls}}
   <div>                                

      {{view Ember.Checkbox checked=updateServerList valueBinding="url"}}{{url}}

   </div>
 {{/each}}

在javascript中:

updateServerList:function(key,value)
{        
    if(value!=undefined)
    {    
        console.log("----1 ", key, value);
    }
}.property(''),

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    根据您的评论,我开始实际使用它并意识到它并不像我想象的那么简单。我最终做的只是构建一个自定义组件,而不是使用Ember.Checkbox

    在您的模板中:

    {{#each}}
        {{check-box url=url}}
    {{/each}}
    

    组件模板:

    <script type='text/x-handlebars' id="components/check-box">
        {{input type="checkbox" checked=toggleURL}}{{url}}
    </script>
    

    组件代码:

    App.CheckBoxComponent = Ember.Component.extend({
        toggleURL: false,
    
        logURL: function() {
            console.log(this.url);
            // Do something with the URL
        }.observes('toggleURL')
    });
    

    【讨论】:

    • 这对我不起作用 - 我仍然无法获取 url 值。我最终改用 Ember 对象建模。
    • 感谢您的推荐。我将 sn-ps 插入到我的文件中。 logURL 观察者为 this.url 返回 null。 ...我必须对我拥有的数据进行建模,然后使用观察者。
    • 有趣。我将它完全插入到我自己的一个 Ember 应用程序中,它按原样工作。我将这样的数组设置为模型:[{url: 'http://google.com', ... ] 用于虚拟数据。尝试在模板调用之外添加{{url}},以查看它是否实际上正在访问该 url。此外,查看您的示例,您将需要使用 each 帮助程序定义的组件,就像我拥有它一样。
    • 哦,它确实有效,是的!我认为,这是一个比我拥有的更好的解决方案!谢谢!
    猜你喜欢
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多