简体   繁体   English

Ember - 如何在控制器上添加/删除数组属性中的元素

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

Given a controller in ember: 给出一个ember中的控制器:

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

You can, for example, set the string property with this.set('stringProp', "Blah blah") . 例如,您可以使用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 . 您正在寻找pushObjectremoveObject等。请参阅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. 对于计算属性和观察者的正确行为,强烈建议您使用这些方法而不是push或其他本机Array方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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