1

I have two components. One has a form it in and the other has navigation where when clicked on the nav button, I need to get access to the form to make sure it's validating before moving on to the next step. However, the form is always undefined when I try to access it from another component. Is it possible to access the form from another component and if so, how can I accomplish this?

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]
});
georgeawg
  • 48,608
  • 13
  • 72
  • 95
RichC
  • 7,829
  • 21
  • 85
  • 149
  • 1
    The form is only defined inside of the `
    ` element. You are trying to access `mainForm` outside of this element, so it is undefined. Take a look at this for some ideas: https://stackoverflow.com/questions/36033940/how-to-pass-data-between-sibling-components-without-using-scope
    – Daniel W. Aug 03 '17 at 21:23
  • Ok but even if I put the other component inside the form element, it still doesn't work, though. https://plnkr.co/edit/3tn2vAitj75hlUnNDjI5?p=preview – RichC Aug 03 '17 at 21:41

1 Answers1

2

One approach is to nest the two components in an ng-form element:

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

And use one-way < binding to connect the two to the top form:

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]
});

The DEMO on PLNKR

georgeawg
  • 48,608
  • 13
  • 72
  • 95