1

I have the following html structure:

<body ng-controller="checkoutController">
    <div class="main clearfix" ng-controller="abroadCourseController">
        //html useless information
        <form name="checkout_form" ng-submit="submitForm()" novalidate>
            <div validate-section="checkoutInfo" ng-show="stepOne">
                //fields
                <div class="confirmationBox">
                    <button type="button" ng-click="displayPaymentMethods()">SHOW PAYMENT METHODS</button>
                </div>
            </div>
            <div validate-section="paymentAbroadCourseB2C" ng-show="stepTwo" >
                //fields
                <div class="confirmationBox">
                    <button type="button" ng-click="submitForm()">FINISH</button>
                </div>
            </div>
        </form>
    </div>
</body>

and the following js:

var myApp = angular.module('myApp',[]);
myApp.controller('checkoutController', function ($scope) {
  $scope.submitForm = function(){
     $scope.stepOne = true;
     $scope.stepTwo = false;

     alert($scope.checkout_form);
     alert('oi');  
  };  
});

myApp.controller('abroadCourseController', function ($scope) {
  $scope.stepOne = true;
  $scope.stepTwo = false;

  $scope.displayPaymentMethods = function(){
     $scope.stepOne = false;
     $scope.stepTwo = true;

     alert($scope.checkout_form);
     alert('oi');  
  };  
});

basically what I need is to have access on checkout_form through the parent controller, however, it's undefined. Is there a way to achieve that?

Here's a JSfiddle: http://jsfiddle.net/thm259o7/

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90

2 Answers2

0

The best way to deal with nested controllers in Angular is using the Controller As Syntax https://toddmotto.com/digging-into-angulars-controller-as-syntax/

Gabriel Matusevich
  • 3,835
  • 10
  • 39
  • 58
0

Yes. Change your code like this:

myApp.controller('abroadCourseController', function ($scope) {
    $scope.form.checkout_form = {}

And then change the HTML like this:

<form name="form.checkout_form" ...>

In my case, It works.

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49