简体   繁体   English

在angular-material md-dialog中使用带有必需^形式的指令

[英]Use directive with required ^form in an angular-material md-dialog

Plunker available here 可以在这里使用 Plunker

I want to include a directive ( first directive in the example) with a required ^form in an angular-material md-dialog. 我想在angular-material md-dialog中包含一个带有必需^form的指令(示例中的第一个指令)。 When the dialog is shown, there's an error saying the form can't be found: 显示对话框时,表示无法找到表单时出错:

[$compile:ctreq] Controller 'form', required by directive 'first', can't be found!

I'm using require:^form because I want to validate with ng-messages some fields of the parent form inside the directive. 我正在使用require:^form因为我想用ng-messages验证指令中父表单的一些字段。 The directive outside the dialog works as expected and the form input is validated correctly, the problem is only happening when I use the directive inside the dialog. 对话框外的指令按预期工作,表单输入正确验证,问题只发生在我在对话框中使用指令时。

I've tried to include another directive ( second directive in the example), without require:^form , and it's displayed correctly in the dialog, but in this case the input cannot be validated. 我试图包含另一个指令(示例中的第二个指令),没有require:^form ,它在对话框中正确显示,但在这种情况下输入无法验证。

My question is: is there any way to use a directive with "form required" in a md-dialog when the form is defined outside of the dialog? 我的问题是:当在对话框外定义表单时,有没有办法在md对话框中使用带有“form required”的指令?

I suppose it's because mdDialog is creating its own scope... I've tried passing the current scope, creating a new one, etc. but without success so far. 我想这是因为mdDialog正在创建自己的范围......我已经尝试传递当前范围,创建一个新范围等等但到目前为止没有成功。

The problem can be easily reproduced in the attached plunker. 这个问题可以很容易地在附带的plunker中重现。

Relevant source for reference: 相关来源参考:

function first() {
    var directive = {
      restrict: 'EA',
      scope: {
        param: '@'
      },
      require: '^form',
      bindToController: true,
      template: '<md-input-container class="md-block"><label>Street with validation</label><input name="street" ng-model="vm.street" required><div ng-messages="form.street.$error"><div ng-message="required">Required</div></div></md-input-container>',
      controller: FirstController,
      controllerAs: 'first',
      link: function(scope, element, attrs, form) {
        scope.form = form;
      }
    };

    FirstController.$inject = [];

    function FirstController() {
      var first = this;
    }
    return directive;
  }


function openDialogFail(event) {
  $mdDialog.show({
    template: '<md-dialog><md-dialog-content><first/></md-dialog-content></md-dialog>',
    targetEvent: event,
    controller: () => this //,
      // scope: $scope.$new()
  });
}

Thanks in advance for your help! 在此先感谢您的帮助!

Extracted from comments: 摘自评论:

I still don't get the use case but does something stop you from wrapping <first /> within a separate <form> like in codepen.io/z00bs/pen/vGaBgm 我仍然没有得到用例但是阻止你将<first />包装在一个单独的<form>就像在codepen.io/z00bs/pen/vGaBgm中一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM