小编Сво*_*Роб的帖子

为什么替换对象不会触发 Angular2 中的更改检测?

我通过服务将对象数组共享给我的组件。因此,有一刻我想用新对象的属性替换数组对象的属性之一(我替换该对象)。因此,我的共享对象应该在使用它的所有模板中更新。

https://plnkr.co/edit/0sRxSivEaEPLsNAJo7MV?p=preview

// my.component.ts
@Component({ 
selector: 'my-component',
template: '<div>MyComponent: {{item.name}}</div>',
})
export class MyComponent implements OnInit {
  constructor(private myService: MyService) {}

  private item = myService.myListData[0];

  ngOnInit() {
    // 1. - This triggers change detection in app.ts template
    this.item.name = 'Name 1111';

    setTimeout(() => {
      // 2. - This doesn't trigger change detection in app.ts template
      let newObj = {name: 'Name 222', age: 225};
      this.item = newObj;
    }, 3000);
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的例子中 //1 更改 app.ts 和 my.component.ts 中的模板值,但 //2 仅在 my.component.ts …

angular2-changedetection angular

5
推荐指数
1
解决办法
3346
查看次数

没有参数值的路由;Angular 路由重定向 - 我可以使用 redirectTo 参数路径吗?

如果可以将 path: ' ' 重定向到 path: ':id' ,我正在徘徊?有没有办法实现这一目标?

谢谢你。

{
  path: 'folder',
  children: [
    {
      path: '',
      redirectTo: ':id',
      pathMatch: 'full',
    },
    {
      path: ':id',
      canActivate: [AuthGuard],
      component: DocumentsComponent,
    },
  ]
}
Run Code Online (Sandbox Code Playgroud)

angular-routing angular2-routing angular

2
推荐指数
1
解决办法
2922
查看次数