0

I was following a question (ui bootstrap modal's controller 'is not defined'), which I then tried modifying for nested controllers, different scoping calls, and renaming/refactoring the code (for learning).

I couldn't get it working in My Plunker, so that leaves me with a few questions:

  1. Why is it not working?

  2. The original question shows creating a new module, rather than appending controllers to the app module -- why? Is this recommended?

  3. If PageController is the ancestor controller, how would it's $scope.open method reference the other controllers open definition?

Code:

var app = angular.module('app', ['ui.bootstrap']);

app.controller('PageController', ['$scope',
  function($scope) {

    $scope.open = function() {
      // how to call ModalController.open?
    };
  }
])
  .controller('ModalController', ['$scope', '$modal', '$log',
    function($scope, $modal, $log) {
      $scope.items = ['item1', 'item2', 'item3'];

      $scope.open = function() {

        var modalInstance = $modal.open({
          templateUrl: 'myModalContent.html',
          controller: 'ModalInstanceCtrl',
          resolve: {
            items: function() {
              return $scope.items;
            }
          }
        });

        modalInstance.result.then(function(selectedItem) {
          $scope.selected = selectedItem;
        }, function() {
          $log.info('Modal dismissed at: ' + new Date());
        });
      };
    }
  ])
  .controller('ModalInstanceCtrl', ['$scope', '$modalInstance', 'items',
    function($scope, $modalInstance, items) {
      $scope.items = items;
      $scope.selected = {
        item: $scope.items[0]
      };

      $scope.ok = function() {
        $modalInstance.close($scope.selected.item);
      };

      $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
      };
    }
  ]);
Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • your first error in the plnkr is that your inline annotation of `$scope` is `scope` when it should be `$scope` check line 3 of your plnkr – David Chase Apr 11 '14 at 15:55
  • @DavidChase Thanks, I missed that oversight. I've updated the code, the modalcontroller trigger now works, but I'll take it out since the post has bigger questions. – vol7ron Apr 11 '14 at 16:00
  • if you want to call your child method from the controller you should abstract those methods into an injectable service or use events like `$broadcast` and `$emit` – David Chase Apr 11 '14 at 16:01
  • @DavidChase if you could put these great comments into an answer (maybe an example), I'd gladly accept it. I plan on reading the docs this weekend, but am trying to get something up and running asap right now. – vol7ron Apr 11 '14 at 16:02

0 Answers0