简体   繁体   English

反转数组时属性未在 Angular 组件中更新

[英]Property not updaated in Angular Component when reversing array

I have an Angular Component which has a public array variable initialized to [] .我有一个 Angular 组件,它有一个初始化为[]的公共数组变量。 In the constructor I am getting a response from a service and updating the array using this.variable = array.reverse() .在构造函数中,我从服务获得响应并使用this.variable = array.reverse()更新数组。 But this doesn't store the reversed array rather the original one.但这并不存储反向数组而是原始数组。 Anything I am missing.我缺少的任何东西。 Thanks in advance.提前致谢。

You need to change the reference to the array, so try您需要更改对数组的引用,因此请尝试

this.variable = [...array].reverse()

you have missed () to reverse() method try this.variable = array.reverse()你错过了 () to reverse() 方法尝试this.variable = array.reverse()

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

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