2

使用 Aurelia,您可以将东西放入容器中,然后注入。容器状态不在对话框中共享。

有没有办法获取我在容器中注入的数据,并在对话框中使用它?


代码示例

主页.js

fetchString(){
  this.data = JSON.parse(stringform);
  this.container.registerInstance('tpscform', this.data);
}

自定义元素.js

import {DialogService} from 'aurelia-dialog';
import {LookupFieldsDialog} from './dialog/lookup-fields-dialog';

@inject('tpscform', DialogService)
export class LookupFieldsQuestion {

  constructor(form, dialog){
    console.log(form); // returns the object from the container - works
    //...
  }

  submit() {
    this.dialogService.open({
      viewModel: LookupFieldsDialog,
      model: this.question,
      lock: false
    });
  }

}

查找字段-dialog.js

import {inject} from 'aurelia-framework';
import {DialogController} from 'aurelia-dialog';

@inject(DialogController, 'tpscform')
export class LookupFieldsDialog {

  constructor(controller, form) {
    this.controller = controller;
    console.log(form); // Returns the string 'tpscform' - doesn't work
  }

  activate(question) {
    this.question = question;
  }

}
4

1 回答 1

1

正如@FabioLuz 提到的,您可以将容器传递到对话框中:

import {Container} from 'aurelia-framework';
import {DialogService} from 'aurelia-dialog';
import {LookupFieldsDialog} from './dialog/lookup-fields-dialog';

@inject(DialogService, Container)
export class LookupFieldsQuestion {

  constructor(dialogService, container) {
    this.container = container;
    this.dialogService = dialogService;
  }

  submit() {
    this.dialogService.open({
      viewModel: LookupFieldsDialog,
      model: this.question,

      // Share current container here
      childContainer: this.container.createChild(),

      lock: false
    });
  }
于 2017-05-02T13:37:07.593 回答