灰烬:设置特定键的数组值 [英] Ember: set array value for specific key

查看:35
本文介绍了灰烬:设置特定键的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Ember中为特定数组键设置值?

How do I set a value for a specific array key in Ember?

我尝试过...

var feed = this.get('controller.feed');
feed[i]['loadingFeedImage'] = false;
this.set('controller.feed', feed);

...但是模板无法识别更改的值.

... but the template doesn't recognize the changed value.

我也尝试过这种方法,但是它不起作用:

I also tried this, but it doesn't work:

this.get('controller.feed['+ i +'].loadingFeedImage', false);

推荐答案

JavaScript对象是基于引用的.因此,使用参考更新来编辑数组中的某些内容会触发观察者更新.

JavaScript objects are reference based. So editing something in an array using it's reference updates triggers an observer update.

您的第一个解决方案不起作用的原因是因为您没有在第二行使用 set .

The reason your first solution does not work is because you don't use set at the second line.

使用余烬数组方法:

// assuming the array is an Ember.Array
this.get('controller.feed').objectAt(i).set('loadingFeedImage', false)

确保您的数组是 Ember.Array

您提供的解决方案使用私有api.我建议您不要使用它.

Your provided solution uses the private api. I would advise you not to use it.

这篇关于灰烬:设置特定键的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆