How to push/pop arrays in Ember.js?

前端 未结 2 712
误落风尘
误落风尘 2021-02-01 14:39

I can include an array in an Ember object, and display the contents using Handlebars. However, I can only replace the array contents using set(). How can I modify the array cont

2条回答
  •  无人及你
    2021-02-01 15:03

    For working with collections, Ember.js provides an Array wrapper class, Ember.Array / Ember.MutableArray

    So, instead of using a plain array, use these:

    // JS
    App.obj = Ember.Object.create({
        "things": Ember.A(["1", "2"])
    });
    App.obj.things.pushObject("3"); // pushObject notifies observers
    
    // HTML + Handlebars
    {{#with App.obj}}
        
      {{#each things}}
    • {{this}}
    • {{/each}}
    {{/with}}

提交回复
热议问题