Praveen Kumar
Praveen Kumar

Reputation: 927

how to use parent controller function or method in model instance in angular js

I am not able to use parent controller in child controller inside modalInstance ,how can we use variable or method in modalInstance child controller. Here is my below code what I have tried so far.

ordercontroller.js

page_controller.controller('open_orderController', [
    '$scope', '$rootScope', '$routeParams', '$http', '$global', '$location', '$filter', '$modal', '$bootbox',
     'DTOptionsBuilder', 'DTColumnBuilder', '$q', '$compile', 
    function($scope, $rootScope, $routeParams, $http, $global, $location, $filter, $modal, $bootbox,DTOptionsBuilder,
        DTColumnBuilder, $q, $compile) {

        $scope.timeFormData = {};

        $scope.my_parent_function = function()
        {

            $scope.timeFormData.text = "hello wordd";
        }       


        $scope.child_controller = function()
        { 
                     var modalInstance = $modal.open({
                    templateUrl: 'time_change.html',
                    controller: function($scope, $modalInstance) {

                        $scope.change_timeFormData = {};


                            $scope.ok = function() {
                                $modalInstance.close();
                            };
                            $scope.cancel = function() {
                                $modalInstance.dismiss('cancel');
                            };



                    },
                    size: 'mg',
                    resolve: {

                    }
                });

        }       

        }]);    

Upvotes: 2

Views: 165

Answers (1)

Manish sharma
Manish sharma

Reputation: 857

I found the solution, To use parent controller variable or method in child controller, add parentScope in your model instance like below.

page_controller.controller('open_orderController', [
    '$scope', '$rootScope', '$routeParams', '$http', '$global', '$location', '$filter', '$modal', '$bootbox',
     'DTOptionsBuilder', 'DTColumnBuilder', '$q', '$compile', 
    function($scope, $rootScope, $routeParams, $http, $global, $location, $filter, $modal, $bootbox,DTOptionsBuilder,
        DTColumnBuilder, $q, $compile) {

  $scope.timeFormData = {};

  $scope.my_parent_function = function()
  {

   $scope.timeFormData.text = "hello wordd";
  }   


  $scope.child_controller = function()
  {


      var modalInstance = $modal.open({
     templateUrl: 'time_change.html',
     controller: function($scope, $modalInstance,parentScope) {

      $scope.change_timeFormData = {};

        $scope.timeFormData.text = 'hello msg will be change like this';

       $scope.ok = function() {
        $modalInstance.close();
       };
       $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
       };



     },
     size: 'mg',
     resolve: {

        parentScope: function()
                                 {
                                    return $scope;
                                },

     }
    });

Upvotes: 1

Related Questions