【问题标题】:Display datas on modal dialog in angular4 application在 Angular 4 应用程序的模态对话框中显示数据
【发布时间】:2017-07-12 21:27:13
【问题描述】:

我有一个 Angular 4 应用程序,我想在对话框中显示数据。所以,我使用@Output 将数据从子组件传递到父组件。

所以,在我的父组件中:

export class DashboardComponent {
    myTask;


    public returnTask(task: any):void {
        console.log("returnTask");
        this.myTask = task;
        console.log(this.myTask);
    }
    openDialogEditTask() {
        console.log(this.myTask);
        let dialogRef = this.dialogEditTask.open(DialogEditTask, {
            //task
            data:  {
                start: this.myTask.start,
                end: this.myTask.end,
                status: this.myTask.status,
                user: this.myTask.user,
                content: this.myTask.content,
                id: this.myTask.id,
                rate: this.myTask.rate
            }
        });
        dialogRef.afterClosed().subscribe(result => {
            this.selectedOption = result;
        });
}
}

在父 html 中,我有:

<visTimeline (myTask)="returnTask($event)"></visTimeline>

在子组件中,我有:

export class VisTimelineComponent {
    @Output() myTask: EventEmitter<any> = new EventEmitter<any>();
}

我用self.myTask.emit(task);发出任务

所以,我在父组件中获取数据(我可以在控制台中看到它们)但我不能在 openDialogEditTask 中使用它们,因为它是未定义的。

那么,您知道如何在调用函数以在对话框中获取数据之前获取数据吗?

编辑: 这是我在子组件中发出数据的代码:

ngOnInit() {
    Timeline.prototype.onTaskDoubleClick = function(task) {
        console.log("Double click on task " + task.id);
        console.log(task);
        $('#editTask').click();
        self.myTask.emit(task);
    };
}

Timeline.prototype.onTaskDoubleClick 是来自库的函数。

【问题讨论】:

  • openDialogEditTask 什么时候被调用?
  • 你如何使用 openDialogEditTask 中的数据,使用可能对数据有帮助的安全运算符?.whatyouwant
  • openDialogEditTask 在点击按钮时被调用:
  • 什么是安全运算符?
  • 您应该仅在任务准备就绪时显示按钮,例如

标签: javascript angular


【解决方案1】:

我认为您无法将数据传递给您的模态组件。尝试使用 componentInstance 方法。

    openDialogEditTask() {
        console.log(this.myTask);
        let dialogRef = this.dialogEditTask.open(DialogEditTask, {
            height: '90%',
            width: '80%'
        });
        dialogRef.componentInstance.myTaskValue = this.myTask; //<- passing data into DialogEditTask component
        dialogRef.afterClosed().subscribe(result => {
            this.selectedOption = result;
        });
}

在您的 DialogEditTask 中声明一个变量 myTaskValue: any;

您将在这个 myTaskValue 变量中获得传递给 DialogEditTask 组件的所有值

【讨论】:

  • 我能够将数据传递到模态组件,但我认为我得到数据太晚了,因为 openDialogEditTask 在 returnTask 之前被调用,所以当我打开对话框时我没有信息但我有就在他们之后。
  • 如何打开 openDialogEditTask() 函数。请添加更多代码。如果您在将 this.openDialogEditTask() 放入 returnTask() 后自动调用它。否则可能是变更检测的问题。在您认为未检测到更改的代码中使用 ngZone。如果可能,请添加代码的 plnkr。 @阿德里安
  • 我通过单击按钮调用 openDialogEditTask()。
  • @Adrien 尝试将 onTaskDoubleClick 的发射放在 ngZone 的角度。这可能是变更检测的问题。检查我的这个答案以了解如何使用 ngZone。我希望它会帮助stackoverflow.com/questions/41096985/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2015-03-22
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
相关资源
最近更新 更多