我通过服务将对象数组共享给我的组件。因此,有一刻我想用新对象的属性替换数组对象的属性之一(我替换该对象)。因此,我的共享对象应该在使用它的所有模板中更新。
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 …
如果可以将 path: ' ' 重定向到 path: ':id' ,我正在徘徊?有没有办法实现这一目标?
谢谢你。
{
path: 'folder',
children: [
{
path: '',
redirectTo: ':id',
pathMatch: 'full',
},
{
path: ':id',
canActivate: [AuthGuard],
component: DocumentsComponent,
},
]
}
Run Code Online (Sandbox Code Playgroud)