我在使用 ionic 1 时遇到问题,我想从第二个 Controller 调用第一个 Controller 中的函数,但我总是遇到此错误
TypeError: $rootScope.getData is not a function
任何人都请帮助我解决这个问题,今晚我必须履行职责,这是我的小脚本。
第一个 Controller :
.controller('FirstController',function('SocketService',$scope, $rootScope){
$rootScope.getData = function(data) {
console.log(data);
}
});
第二个 Controller :
.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();
}
})
关于javascript - 来自另一个 Controller 的 Ionic 1 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48336949/