小编Dan*_*cos的帖子

在订阅中更改变量后,不会更新Angular 6 View

当变量在订阅中发生变化时,为什么视图没有更新?

我有这个代码:

example.component.ts

testVariable: string;

ngOnInit() {
    this.testVariable = 'foo';

    this.someService.someObservable.subscribe(
        () => console.log('success'),
        (error) => console.log('error', error),
        () => {
            this.testVariable += '-bar';

            console.log('completed', this.testVariable);
            // prints: foo-Hello-bar
        }
    );

    this.testVariable += '-Hello';
}
Run Code Online (Sandbox Code Playgroud)

example.component.html

{{testVariable}}
Run Code Online (Sandbox Code Playgroud)

但视图显示:foo-Hello.

为什么不显示:foo-Hello-bar

如果我ChangeDetectorRef.detectChanges()在订阅中调用它将显示正确的值,但为什么我必须这样做?

我不应该从每个订阅中调用此方法,或者根本不应该(angular应该处理这个).有正确的方法吗?

我是否遗漏了Angular/rxjs 5到6的更新内容?

现在我有Angular版本6.0.2和rxjs 6.0.0.相同的代码在Angular 5.2和rxjs 5.5.10中正常工作,无需调用detectChanges.

subscribe observable angular6 angular-changedetection rxjs6

20
推荐指数
3
解决办法
2万
查看次数

如何在Angular项目中正确构造CRUD路线?

问题:是否存在定义单页应用程序路由的最佳实践?

在Angular项目中,功能通常在延迟加载的模块中分开,然后在路由AppRoutingModule和延迟加载的模块中配置路由。

假设该应用将管理目录,例如:产品。路由可以这样配置:

选项1:

  • 清单: /products
  • 创建: /products/create
  • 读: /products/:id
  • 更新: /products/:id/edit

它可以工作,但看起来有点混乱,并且和之间有些歧义/products/:id/products/create因为参数:id可以匹配字符串“ create”。示例代码:

app-routing.module.ts:

const routes: Routes = [
    {
        path: '',
        children: [
            { path: 'products', loadChildren: 'app/products/products.module#ProductsModule' },
        ]
    }
];
Run Code Online (Sandbox Code Playgroud)

products-routing.module.ts

const routes: Routes = [
    { path: '', component: ListProductsComponent },
    { path: 'create', component: CreateProductComponent },
    { path: ':id', component: ViewProductComponent },
    { path: ':id/edit', component: EditProductComponent },
];
Run Code Online (Sandbox Code Playgroud)

选项2

  • 清单: /products
  • 创建: /products/create
  • 阅读:( …

routes lazy-loading angular-cli angular angular-router

6
推荐指数
1
解决办法
1920
查看次数