简体   繁体   中英

Ember - How to add/remove elements from an array property on controller

Given a controller in ember:

export default Ember.Controller.extend({
  stringProp: "",
  arrayProp: []
});

You can, for example, set the string property with this.set('stringProp', "Blah blah") . But that is overriding. What I want to do is push to the array property.

Is there a better (either shorter or faster) way than this:

this.set('arrayProp', this.get('arrayProp').push(element));

Also, is there a shortcut for removing elements from such an array property?

You are looking for pushObject , removeObject , etc. See http://emberjs.com/api/classes/Ember.MutableArray.html .

this.get('arrayProp').pushObject(element);

For correct behavior by computed properties and observers, it is strongly recommended you use these methods instead of push or other native Array methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM