How to get form data in ember.js?

by raphael_tillman , in category: Javascript , 3 months ago

How to get form data in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , 3 months ago

@raphael_tillman 

To get form data in Ember.js, you can use the Ember.get method to retrieve the form values from the input fields in the template.


Here is an example of how you can get form data in Ember.js:

  1. Create a template with a form that contains input fields:
1
2
3
4
5
6
7
8
9
<form {{action "submitForm" on="submit"}}>
  <label for="firstName">First Name:</label>
  {{input type="text" id="firstName" value=firstName}}

  <label for="lastName">Last Name:</label>
  {{input type="text" id="lastName" value=lastName}}

  <button type="submit">Submit</button>
</form>


  1. In the corresponding controller or component, define the submitForm action to handle form submission:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import Controller from '@ember/controller';

export default Controller.extend({
  actions: {
    submitForm() {
      let firstName = this.get('firstName');
      let lastName = this.get('lastName');

      console.log('First Name: ' + firstName);
      console.log('Last Name: ' + lastName);
    }
  }
});


In this example, when the form is submitted, the submitForm action is triggered, and it retrieves the values of the firstName and lastName input fields using this.get(). You can then use the form data as needed in your application.