在Angular中,如果想要在刷新数组列表后将新对象推送到列表中,可以使用push()
方法。下面是一个示例代码:
组件类:
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
template: `
- {{ item }}
`
})
export class ExampleComponent {
itemList: string[] = ['对象1', '对象2', '对象3'];
refreshList() {
// 模拟刷新列表后返回的新对象
const newObject = '新对象';
// 将新对象推送到数组列表中
this.itemList.push(newObject);
}
}
在上述示例中,itemList
是一个字符串数组,其中包含了一些初始对象。refreshList()
方法模拟刷新列表后返回的新对象,并使用push()
方法将新对象添加到itemList
数组中。在模板中使用*ngFor
指令遍历itemList
数组,并将每个对象显示为列表项。最后,通过点击按钮触发refreshList()
方法来刷新列表并将新对象推送到数组中。