繁体   English   中英

来自另一个控制器的Ionic 1呼叫功能

[英]Ionic 1 call function from another controller

我在使用ionic 1时遇到问题,我想从Second Controller调用First Controller中的函数,但是我总是会收到此错误

TypeError: $rootScope.getData is not a function 

任何人都请帮助我解决这个问题,今晚我必须履行职责,这是我的小脚本。

第一控制人:

.controller('FirstController',function('SocketService',$scope, $rootScope){
  $rootScope.getData = function(data) {
            console.log(data);
        }
});

第二控制器:

    .controller('SecondController',function('SocketService',$scope, $rootScope){
      $scope.callData = function() {
                $rootScope.getData('success');
            }
       $scope.callData();
    });

请任何人帮助我。 谢谢。

我建议您使用一项服务

.service('CommunFunctionsService', function(){
    var s = {};
    s.getData = function() {
        // do something
    }
    return s;
})

.controller('FirstCtrl', function(CommonFunctionsService, $scope){
    $scope.getData = function(){
        CommunFunctionsService.getData();
    }
})

.controller('SecondController', function(CommonFunctionsService, $scope){
    $scope.getDataToo= function(){
        CommunFunctionsService.getData();
    }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM