Angular 如果要重复数组,如何获取ng?

Angular 如果要重复数组,如何获取ng?,angular,Angular,我有一个元素,其上的*ng表示。它完美地呈现了数据(对象数组)。如果我添加了一个元素,那么我会在视图中看到新数据 <div *ng-for="#item of items" [attr.bob]="item.bob" /> 如果我修改了项目列表中的一个项目,我需要它反映在我的视图中,但它不是。我怎样才能做到这一点?我可以触发整个阵列的完整渲染-我可以通过添加/删除元素-但是这里的最佳实践是什么?也许有一种方法可以“弄脏”物品 编辑:我将代码改回了无法使用的原始版本,因为我刚刚发

我有一个元素,其上的
*ng表示
。它完美地呈现了数据(对象数组)。如果我添加了一个元素,那么我会在视图中看到新数据

<div *ng-for="#item of items" [attr.bob]="item.bob" />

如果我修改了项目列表中的一个项目,我需要它反映在我的视图中,但它不是。我怎样才能做到这一点?我可以触发整个阵列的完整渲染-我可以通过添加/删除元素-但是这里的最佳实践是什么?也许有一种方法可以“弄脏”物品


编辑:我将代码改回了无法使用的原始版本,因为我刚刚发布的版本确实有效,使这个问题无效我仍然很好奇您是否可以手动强制重新迭代,因此我将这个问题留待讨论。如果您在这里寻找解决方案,我所做的是使某些内容直接依赖于属性(索引),而不是嵌套在数组中的对象中。

您不能在某个地方设置[attr.something]的值,然后强制刷新吗?

您不能设置[attr.something]的值吗然后强制刷新的某个地方?

您不能将[attr.something]的值设置为强制刷新的某个地方吗?

您不能将[attr.something]的值设置为强制刷新的某个地方吗?

实际上不需要重新迭代。属性绑定到数组的元素,并将分别更改。阵列的更改也将自动显示

function RandomColorHex(){
  return '#'+Math.floor(Math.random()*16777215).toString(16);
}

@Component({
    selector: 'app'
})
@View({
  template: `
    <button (click)="push()">add item</button>
    <p *ng-for="#item of items" [style.color]="item.color">{{ item.title }}</p>
  `,
  directives: [NgFor]
})
class AppComponent{
  constructor(){
    this.items = [
      {title:"foo", color:"red"},
      {title:"bar", color:"green"},
      {title:"baz", color:"blue"}
    ];

    setInterval(() => {
      let randomElement = this.items[Math.floor(Math.random()*this.items.length)];
      randomElement.color = RandomColorHex();
    }, 500);
  }
  push(){
    this.items.push({title:"i'm new here", color:"black"});
  }
}
函数RandomColorHex(){ 返回“#”+Math.floor(Math.random()*16777215).toString(16); } @组成部分({ 选择器:“应用程序” }) @看法({ 模板:` 添加项 {{item.title}

`, 指令:[NgFor] }) 类AppComponent{ 构造函数(){ 此项。项目=[ {标题:“foo”,颜色:“red”}, {标题:“条”,颜色:“绿色”}, {标题:“baz”,颜色:“蓝色”} ]; 设置间隔(()=>{ 让randomElement=this.items[Math.floor(Math.random()*this.items.length)]; randomElement.color=RandomColorHex(); }, 500); } 推{ this.items.push({标题:“我是新来的”,颜色:“黑色”}); } }
实际上不需要重新迭代。属性绑定到数组的元素,并将分别更改。阵列的更改也将自动显示

function RandomColorHex(){
  return '#'+Math.floor(Math.random()*16777215).toString(16);
}

@Component({
    selector: 'app'
})
@View({
  template: `
    <button (click)="push()">add item</button>
    <p *ng-for="#item of items" [style.color]="item.color">{{ item.title }}</p>
  `,
  directives: [NgFor]
})
class AppComponent{
  constructor(){
    this.items = [
      {title:"foo", color:"red"},
      {title:"bar", color:"green"},
      {title:"baz", color:"blue"}
    ];

    setInterval(() => {
      let randomElement = this.items[Math.floor(Math.random()*this.items.length)];
      randomElement.color = RandomColorHex();
    }, 500);
  }
  push(){
    this.items.push({title:"i'm new here", color:"black"});
  }
}
函数RandomColorHex(){ 返回“#”+Math.floor(Math.random()*16777215).toString(16); } @组成部分({ 选择器:“应用程序” }) @看法({ 模板:` 添加项 {{item.title}

`, 指令:[NgFor] }) 类AppComponent{ 构造函数(){ 此项。项目=[ {标题:“foo”,颜色:“red”}, {标题:“条”,颜色:“绿色”}, {标题:“baz”,颜色:“蓝色”} ]; 设置间隔(()=>{ 让randomElement=this.items[Math.floor(Math.random()*this.items.length)]; randomElement.color=RandomColorHex(); }, 500); } 推{ this.items.push({标题:“我是新来的”,颜色:“黑色”}); } }
实际上不需要重新迭代。属性绑定到数组的元素,并将分别更改。阵列的更改也将自动显示

function RandomColorHex(){
  return '#'+Math.floor(Math.random()*16777215).toString(16);
}

@Component({
    selector: 'app'
})
@View({
  template: `
    <button (click)="push()">add item</button>
    <p *ng-for="#item of items" [style.color]="item.color">{{ item.title }}</p>
  `,
  directives: [NgFor]
})
class AppComponent{
  constructor(){
    this.items = [
      {title:"foo", color:"red"},
      {title:"bar", color:"green"},
      {title:"baz", color:"blue"}
    ];

    setInterval(() => {
      let randomElement = this.items[Math.floor(Math.random()*this.items.length)];
      randomElement.color = RandomColorHex();
    }, 500);
  }
  push(){
    this.items.push({title:"i'm new here", color:"black"});
  }
}
函数RandomColorHex(){ 返回“#”+Math.floor(Math.random()*16777215).toString(16); } @组成部分({ 选择器:“应用程序” }) @看法({ 模板:` 添加项 {{item.title}

`, 指令:[NgFor] }) 类AppComponent{ 构造函数(){ 此项。项目=[ {标题:“foo”,颜色:“red”}, {标题:“条”,颜色:“绿色”}, {标题:“baz”,颜色:“蓝色”} ]; 设置间隔(()=>{ 让randomElement=this.items[Math.floor(Math.random()*this.items.length)]; randomElement.color=RandomColorHex(); }, 500); } 推{ this.items.push({标题:“我是新来的”,颜色:“黑色”}); } }
实际上不需要重新迭代。属性绑定到数组的元素,并将分别更改。阵列的更改也将自动显示

function RandomColorHex(){
  return '#'+Math.floor(Math.random()*16777215).toString(16);
}

@Component({
    selector: 'app'
})
@View({
  template: `
    <button (click)="push()">add item</button>
    <p *ng-for="#item of items" [style.color]="item.color">{{ item.title }}</p>
  `,
  directives: [NgFor]
})
class AppComponent{
  constructor(){
    this.items = [
      {title:"foo", color:"red"},
      {title:"bar", color:"green"},
      {title:"baz", color:"blue"}
    ];

    setInterval(() => {
      let randomElement = this.items[Math.floor(Math.random()*this.items.length)];
      randomElement.color = RandomColorHex();
    }, 500);
  }
  push(){
    this.items.push({title:"i'm new here", color:"black"});
  }
}
函数RandomColorHex(){ 返回“#”+Math.floor(Math.random()*16777215).toString(16); } @组成部分({ 选择器:“应用程序” }) @看法({ 模板:` 添加项 {{item.title}

`, 指令:[NgFor] }) 类AppComponent{ 构造函数(){ 此项。项目=[ {标题:“foo”,颜色:“red”}, {标题:“条”,颜色:“绿色”}, {标题:“baz”,颜色:“蓝色”} ]; 设置间隔(()=>{ 让randomElement=this.items[Math.floor(Math.random()*this.items.length)]; randomElement.color=RandomColorHex(); }, 500); } 推{ this.items.push({标题:“我是新来的”,颜色:“黑色”}); } }

这实际上就是您评论的代码所做的,结果是——我的代码已经在工作了。我对你的答案投了赞成票,因为这实际上是对我新帖子的回答;)这实际上就是你评论的代码所做的,结果是——我的代码已经在工作了。我对你的答案投了赞成票,因为这实际上是对我新帖子的回答;)这实际上就是你评论的代码所做的,结果是——我的代码已经在工作了。我对你的答案投了赞成票,因为这实际上是对我新帖子的回答;)这实际上就是你评论的代码所做的,结果是——我的代码已经在工作了。我对你的答案投了赞成票,因为这实际上是对我新帖子的回答;)我发誓我尝试过(修改数组中元素的属性)但失败了。我不知道为什么它当时不起作用,但现在起作用了。我发誓我曾经尝试过(修改数组中某个元素的属性),但失败了。我不知道为什么它当时不起作用,但现在起作用了。我发誓我已经尝试过了(修改元素上的属性)