簡體   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