javascript - 从另一个不同的 AngularJS 组件访问表单(用于验证)

标签 javascript angularjs angularjs-ng-form

我有两个组件。一个有一个表单,另一个有导航,当单击导航按钮时,我需要访问该表单以确保它在继续下一步之前进行验证。但是,当我尝试从另一个组件访问表单时,该表单始终是未定义的。是否可以从另一个组件访问表单,如果可以,我该如何实现这一点?

enter image description here

Here's my sample code in plunker

Index.html

<!DOCTYPE html>
<html>

<head>
  <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="app">

  <form-component></form-component>
  <nav-component></nav-component>
</body>

</html>

script.js

// Code goes here

function checkForm(form) {
  if (form) {
      if (form.$valid) {
        alert("Form is valid!");
      } 
      else
      {
        alert("Form is invalid!");
      }
    }
    else {
      alert("FAILED! Form is undefined!");
    }
}

function formController() {
  var model = this;

  model.goToNextStep = function(form) {
    model.form = form;
    checkForm(form);
  };

}

function navController() {
  var model = this;

  model.goToNextStep = function(form) {
    model.form = form;
    checkForm(form);
  };

}
var app = angular.module("app", []);

app.component("formComponent", {
    template: `
      <div class="container">
        <form name="mainForm">
          <p>When you click the button that exists in the same component
             as the form, everything works fine.\
          </p>
          <input type="text" name="test123" value="THIS IS A TEST" />
          <button type="button" ng-click="model.goToNextStep(mainForm);">
            Submit Form
          </button>
        </form>
        <br /><strong>formComponent model:</strong> <pre>{{model | json }}</pre>
      </div>
    `,
    controllerAs: "model",
    controller: [formController]
});


app.component("navComponent", {
    template: `
      <div class="container">
        <p>When you click the button that does NOT exist in the same
           component as the form, it doesn't work.
        </p>
        <button type="button" ng-click="model.goToNextStep(mainForm);">Submit Form</button>
        <br /><br /><strong>navComponent model:</strong>
        <pre>{{model | json }}</pre>
      </div>
    `,
    controllerAs: "model",
    controller: [navController]
});

最佳答案

一种方法是将两个组件嵌套在 ng-form element 中:

  <ng-form name="top">
    <form-component top-form="top"></form-component>
    <nav-component top-form="top"></nav-component>
  </ng-form>

并使用单向 <绑定(bind)将两者连接到顶部表单:

app.component("formComponent", {
    bindings: {topForm: "<"},
    template: `
      <div class="container">
        <form name="mainForm">
          <p>When you click the button that exists in the same component as the form, everything works fine.</p>
          <input type="text" name="test123" value="THIS IS A TEST" />
          <button type="button" ng-click="model.goToNextStep(model.topForm);">Submit Form</button>
        </form>
        <br /><strong>formComponent model:</strong> <pre>{{model | json }}</pre>
      </div>
    `,
    controllerAs: "model",
    controller: [formController]
});
app.component("navComponent", {
    bindings: {topForm: '<'},
    template: `
      <div class="container">
        <p>When you click the button that does NOT exist in the same component as the form, it doesn't work.</p>
        <button type="button" ng-click="model.goToNextStep(model.topForm);">Submit Form</button>
        <br /><br /><strong>navComponent model:</strong> <pre>{{model | json }}</pre>
      </div>
    `,
    controllerAs: "model",
    controller: [navController]
});

DEMO on PLNKR

关于javascript - 从另一个不同的 AngularJS 组件访问表单(用于验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45494399/

相关文章:

javascript - 浏览器中的代码在 Dreamweaver CC 中不起作用

javascript - React JS 中在特定 Prop 更改时调用组件方法的正确模式是什么?

angularjs - 将 AngularJS 嵌套表单设置为已提交

javascript - Angularjs 或 Ionic 框架中的多语言应用程序

javascript - 使用 D3.js 的实时图表

带有 ng-repeat 验证的 AngularJS 多个表单

javascript - 延迟表单提交(AngularJS)

javascript - 如何正确处理 ReactJS 中的状态变化

javascript - 如何通过javascript获取一个人的指纹

javascript - Angular/Karma - 当预期请求和观察到的请求相同时出现 "Unexpected Request"错误