2017-03-09 81 views
2

我不能在子控制器内部使用父控制器modalInstance,我们如何在modalInstance子控制器中使用变量或方法。这是我的下面的代码,迄今为止我尝试过。如何在角度js中使用父控制器函数或模型实例中的方法

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: { 

        } 
       }); 

     }  

     }]);  

回答

1

我找到了解决办法,要使用家长控制变量或方法在儿童控制器,在你的模型实例添加parentScope像下面。

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; 
           }, 

    } 
    }); 
+0

这是工作你的代码,thanx manish帮我 –

+0

欢迎! Praveen Kumar –

+0

工程就像一个魅力! –

相关问题