How to set default value in ember.js?

Member

by brandy , in category: Javascript , 3 months ago

How to set default value in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 3 months ago

@brandy 

In Ember.js, you can set default values for properties in your components or controllers by using the init hook. Here's an example of how to set a default value for a property in a component:

1
2
3
4
5
6
7
8
9
import Ember from 'ember';

export default Ember.Component.extend({
  init() {
    this._super(...arguments);
    
    this.set('myProperty', 'default value');
  }
});


In this example, the myProperty property is being set to a default value of 'default value' when the component is initialized.


You can also set default values for properties in controllers by using the init hook in a similar way:

1
2
3
4
5
6
7
8
9
import Ember from 'ember';

export default Ember.Controller.extend({
  init() {
    this._super(...arguments);
    
    this.set('myProperty', 'default value');
  }
});


By setting default values in the init hook, you ensure that the property is initialized with the default value when the component or controller is created.