Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度子路径观测器_Angular_Angular5_Angular Routing - Fatal编程技术网

Angular 角度子路径观测器

Angular 角度子路径观测器,angular,angular5,angular-routing,Angular,Angular5,Angular Routing,在Angular5应用程序中,我的路由定义如下: path: 'my-object/:id', component: MyObjectDetailComponent, children: [ {path: '', redirectTo: 'sublist', pathMatch: 'full'}, {path: 'sublist', component: PointComponent}, {path: 'new', componen

在Angular5应用程序中,我的路由定义如下:

path: 'my-object/:id',
    component: MyObjectDetailComponent,
    children: [
        {path: '', redirectTo: 'sublist', pathMatch: 'full'},
        {path: 'sublist', component: PointComponent},
        {path: 'new', component: PointFormComponent},
    ],
在MyObjectDetailComponent模板文件中,我有:

<button #new />
<router-outlet></router-outlet>

当我向用户显示PointForm时,我需要隐藏一些界面对象(如new button)

我相信我需要在url或路由中添加一些观察者,但不知道如何(以及在哪里)做到这一点


当用户输入PointFormComponent(路径:new)时,它应该隐藏
(和一些其他UI元素),并在用户离开时再次显示这些元素。

您可以在MyObjectDetailComponent中添加类似的内容:

import { Router } from '@angular/router';

export class MyObjectDetailComponent implements OnInit {

    currentUrl: string;

    constructor(private router:Router) {  }

    ngOnInit() {
        // that´s how you get the current url
        currentUrl = this.router.url; 
    }
}
并在模板中添加类似的内容

<button *ngIf="currentUrl == ![add path to "new" url here]" #new />
<router-outlet></router-outlet>

您需要这样做,这意味着您需要检查路由更改,如果更改的路由中包含新路由,则将hideflag设置为true并隐藏contorls

class MyClass {
  hideInputs = false;
  constructor(private router: Router) {
    router.events.subscribe((val) => {
      const href = window.location.href;
       this.hideInputs = href.endsWith('/new');
    });
  }
}


<button *ngIf="hideInputs " #new />
class-MyClass{
hideInputs=false;
构造函数(专用路由器:路由器){
router.events.subscribe((val)=>{
const href=window.location.href;
this.hideInputs=href.endsWith('/new');
});
}
}

所以当url
http://domain//new
?是的,它确实是:“domain/object/:id/new”尝试一下我的答案可能对您有效这不起作用,因为我切换子路由时不会触发OnInit。工作非常完美,非常简单:)我刚刚更改了
includes('new')
href.endsWith('/new'))
和'hideInputs'用于编辑@Robert-好的,这基本上是个好概念,订阅路由事件并执行活动