如何在AngularJS中从Controller-B调用Controller-A中的函数

时间:2014-11-28 10:10:51

标签: angularjs

我的代码就像这样

Controller.JS
    angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', function($scope, rateRequestService) {
    $scope.rateData = [];

    rateRequestService.getData().success(function(response) {
        $scope.rateData = response;
    }).error(function (data, status, headers, config) {

         $scope.openModal();
    });
});

angular.module('RateRequestApp.controllers').controller('ModalDemoCtrl', function($scope, $modal, $log) {


    $scope.openModal = function(size) {

        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,

        });
    };


});


App.JS

    angular.module('RateRequestApp', [
   'RateRequestApp.services',
   'RateRequestApp.controllers',
   'ui.bootstrap'
]);

正如您所看到的,我正在尝试在另一个控制器中调用函数$scope.openModal();,显然它会抛出错误

TypeError: undefined is not a function

在第

   $scope.openModal();

有没有办法克服这个问题?

4 个答案:

答案 0 :(得分:2)

由于控制器不可注射,我不会尝试。请尝试利用共享服务。

答案 1 :(得分:1)

您可以使用角度控制器服务$ controller

将控制器注入另一个控制器
angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', ['$controller','$scope', function($scope, rateRequestService,$controller) {
    var modalDCtrl = $scope.$new();
    $controller('ModalDemoCtrl',{$scope : modalDCtrl });
    $scope.rateData = [];
    rateRequestService.getData().success(function(response) {
        $scope.rateData = response;
    }).error(function (data, status, headers, config) {
        modalDCtrl.openModal();
    });
}]);

但是,您还需要将方法附加到控制器实例而不是$ scope。

angular.module('RateRequestApp.controllers').controller('ModalDemoCtrl',  function($scope, $modal, $log) {
    this.openModal = function(size) {
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
        });
    };
});

参考:How do I inject a controller into another controller in AngularJS

答案 2 :(得分:0)

在应用程序中定义控制器,然后根据需要注入它们。您尚未提及模型实例控制器的实现。所以我假设你忘了实施它。

angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', function($scope, rateRequestService) { 
                // ReadOnlyController's logic
}));

angular.module('RateRequestApp.controllers').controller('ModalDemoCtrl', function($scope, $modal, $log) { 

/**
 *  This might be missing
 */
var ModalInstanceCtrl = function ($scope, $modal) {
            // ModalInstanceController's logic
};

$scope.openModal = function(size) {

        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: ModalInstanceCtrl,
            size: size,

        });
    };

}));



App.JS

    angular.module('RateRequestApp', [
   'RateRequestApp.services',
   'RateRequestApp.controllers',
   'ui.bootstrap'
]);

希望上面的片段会有所帮助:)

答案 3 :(得分:0)

$ controller会有所帮助,试试这个链接https://docs.angularjs.org/guide/controller你会得到一个好主意。