【问题标题】:Execute particular component after click on md dialog button单击 md 对话框按钮后执行特定组件
【发布时间】:2017-06-04 09:32:02
【问题描述】:

我在页面的左侧面板上有 8 个图形图像。当我拖动这些图像并放在右侧面板上时,我正在显示 MD 对话框。在 md 对话框中,我在下拉菜单中显示列表。

我推荐了this code

从下拉列表中选择项目后,我单击 md 对话框的“是”按钮。

在此按钮上单击我想根据项目选择执行特定组件。例如,如果我选择“Heap Memory”,那么我想执行“HeapMemoryComponent”等。

路由.ts

import  {ModuleWithProviders} from '@angular/core';
import {Routes,RouterModule} from '@angular/router';

import {AppComponent} from './app.component';
import {HeapMemoryComponent} from './aem-down-time-graph/aem-down-time-graph.component'
const appRoutes:Routes = [
{
 path: 'abc',
 component: HeapMemoryComponent
}
];
export const routing:ModuleWithProviders = RouterModule.forRoot(appRoutes);

左面板组件

import { Component, OnInit } from '@angular/core';
import {LeftpanelService} from "../leftpanel.service"
import {Leftpanel} from "../leftpanel";
import {MdDialog} from "@angular/material";
import {MdDialogRef} from "@angular/material";
import { Routes, RouterModule } from '@angular/router';
import {ServersListDialogComponent} from "../servers-list-dialog/servers-list-dialog.component";
@Component({
selector: 'app-leftpanel',
templateUrl: './leftpanel.component.html',
styleUrls: ['./leftpanel.component.css']
})
export class LeftpanelComponent implements OnInit {
 dialogRef: MdDialogRef<ServersListDialogComponent>;
 leftpanels:Leftpanel[];
 receivedData:Array<any> = [];

 constructor(
   public dialog: MdDialog,private service:LeftpanelService,public router:Router
 ) { }

 ngOnInit() {
   this.service.getLeftpanels().subscribe(lst =>this.leftpanels=lst);
 }

 transferDataSuccess($event) {
 this.receivedData.push($event.dragData);
 this.openDialog();
 }
 openDialog() {
  this.dialogRef = this.dialog.open(ServersListDialogComponent, {
    disableClose: false
  });

  this.dialogRef.afterClosed().subscribe(result => {
    console.log('result: ' + result);
    this.dialogRef = null;
    if(result.componentName == "HeapMemoryComponent"){
      this.router.navigate(['HeapMemoryComponent']);
    }
  });
 }

}

点击“是”后,我想根据项目选择执行以下组件。

@Component({
 selector: 'app-mainpanel',
 templateUrl: './heap-memory.component.html',
 styleUrls: ['heap-memory.component.css']
})
export class  HeapMemoryComponent implements OnInit {
constructor() {
 console.log("HeapMemoryComponent object is created...");
}

ngOnInit() {
 console.log("HeapMemoryComponent ....");
}

}

HeapMemoryComponent 上面有一些 html 响应,上面的这个组件将有服务。而且我会有多个像上面这样的组件。

我不知道如何像上面的特定组件那样执行? 请给我解决方案

【问题讨论】:

  • 您的意思是导航到特定组件吗?
  • @echonax。是的,我想根据项目选择导航特定组件。

标签: angular angular2-services angular2-directives


【解决方案1】:

如果您想在对话结果之后导航到特定组件,您可以执行以下操作:

import {Router} from "@angular/router";
...
constructor(public router:Router..){}

this.dialogRef.afterClosed().subscribe(result => {
    console.log('result: ' + result);
    this.dialogRef = null;
    //based on result
    if(result.componentName == "HeapMemoryComponent"){
      this.router.navigate(['abc']);
    }
  });

注意result.componentName部分是我编的,不知道你在回调中得到什么结果。

【讨论】:

  • 谢谢你,@echonax。我收到“找不到名称‘路由器’”错误。
  • @AnilJagtap 嗯,你有没有像我在constructor 中那样添加router:Router
  • 我在构造函数中添加了。
  • @AnilJagtap 哦,我猜你没有导入Router。更新了我的答案。
  • 我导入了上面的库。出现以下错误:“重复标识符'Promise'”和“找不到名称'Router'”。我错过了什么?
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多