angular - 单击 md 对话框按钮后执行特定组件

标签 angular angular2-services angular2-directives

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

enter image description here

我提到了 this code

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

在此按钮上单击“我想根据项目选择执行特定组件”。例如,如果我选择“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']);
    }
  });
 }

}

单击"is"后,我想根据项目选择执行以下组件。

@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 响应,上面的组件将有服务。我将有多个像上面这样的组件。

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

最佳答案

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

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 部分,我不知道您在回调中得到什么结果。

关于angular - 单击 md 对话框按钮后执行特定组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745944/

相关文章:

javascript - angular2 zone.js 进行自动 sock.js 调用

javascript - 如何在 angular 2.0 中创建拖动属性指令?

angular - angular4中的浏览器关闭事件

angular - 如何在 Angular 2 应用程序中配置不同的开发环境

javascript - html 无法在表单中显示来自 REST api 的数据后,生命周期 Hook OnInit 加载?

angular - 应用 Angular2 ngModel 后格式化输入值

oop - 如何在 Typescript 中将值从一个类的方法传递到另一个类的方法?

Angular:Async Await- promise 中的 EventListener

javascript - ExpressionChangedAfterItHasBeenCheckedError : Expression has changed after it was checked. 先前值 : 'ngIf: false' . 当前值: 'ngIf: true'

angular - 如何订阅一次事件发射器?