How to get the value of a checkbox of a template in the controller in ember app

假装没事ソ 提交于 2020-01-04 04:44:06

问题


I am trying to find whether the checkbox is checked or not in a controller.

Here's my template:

<script type="text/x-handlebars">
  {{view Ember.TextField valueBinding="firstname" placeholder="First Name"}}
  <input type="checkbox" name="remember_me"> Remember me </input>
  <button {{action save }}>Save</button>
</script>

Here's my controller:

App = Ember.Application.create();
App.ApplicationController = Ember.Controller.extend({
    save: function(){
//need to get the value of "remember_me" here
      alert(this.get("firstname"));
    }
});

How can I get the value of "remember_me" (whether it's checked or not) in the controller. Can I do valueBinding on the check box. If so , can you please give me an example syntax.

jsfiddle:

http://jsfiddle.net/Rtd4d/


回答1:


You should probably use the input helper that ember provides (see the docs).

{{input type="checkbox" checked=remember_me}}

To get the model that is set on a controller, use this.get('model').

So, to get the remember_me attribute from the model, it's simply

this.get('model').get('remember_me')

Assuming remember_me is a boolean attribute, this should return true or false.

See the jsbin.


EDIT

I didn't realize that by default the controller will delegate to it's model, so

this.get('remember_me')

should work.



来源:https://stackoverflow.com/questions/20668285/how-to-get-the-value-of-a-checkbox-of-a-template-in-the-controller-in-ember-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!