2017-04-26 38 views
0

我正在研究一个离子项目,我试图从控制器调用工厂方法。这个工厂方法是在一个单独的文件中。当这样做时,我收到以下错误。

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- loginService 

这里是我的文件:

services.js

angular.module('starter.services', ['starter.constants']) 
.factory('loginService', function($scope, $http,constants) { 
var lgurl = constants.BASE_URL+constants.User_Login; 
return { 
loginXmanager: function(username,password,deviceID,deviceType){ 
/*Demo*/ 
    return $http.post(lgurl).then(function(response){ 
    users = response; 
    return users; 
    }); 
    } 
} 
}); 

controllers.js

angular.module('starter.controllers', ['starter.services']) 

.controller('AppCtrl',['$scope', 'loginService',function($scope, 
$ionicModal, $timeout,loginService) { 


// Form data for the login modal 
$scope.loginData = {}; 

// Create the login modal that we will use later 
$ionicModal.fromTemplateUrl('templates/login.html', { 
    scope: $scope 
}).then(function(modal) { 
    $scope.modal = modal; 
}); 

// Triggered in the login modal to close it 
    $scope.closeLogin = function() { 
    $scope.modal.hide(); 
}; 

// Open the login modal 
    $scope.login = function() { 
    $scope.modal.show(); 
}; 


$scope.doLogin = function() { 
    console.log('Doing login'); 
    var usrnm = $scope.loginData.username; 
    var pass = $scope.loginData.password; 
    var deviceID = "1234"; 
    var deviceType = "any"; 
    console.log('username - '+usrnm); 
    console.log('password - '+pass); 
    if (loginService) { 
    loginService.loginXmanager(usrnm,pass,deviceID,deviceType); 
    }else{ 
    console.log("loginService error"); 
} 

}; 
}]) 

什么似乎是这里的问题的任何帮助表示赞赏。

+1

$范围仅用于与视图连接......一个工厂没有做到这一点 – charlietfl

回答

2

你得到这个错误,因为你不能在工厂注入$scope。因此,请将您的工厂更改为:

.factory('loginService', function($http, constants) { 

另外,在您的控制器中,注射未完全提供且出现故障。它应该是如下:

.controller('AppCtrl', ['$scope', 'loginService', '$timeout', '$ionicModal', 
    function($scope, loginService, $timeout, $ionicModal) { 
1

不能在工厂内使用范围变量。从工厂卸下scope注射器

变化这

.factory('loginService', function($scope, $http,constants) { 

.factory('loginService', function($http,constants) { 

此外,在控制器注入服务时作为串值

变化遵循的格式此

.controller('AppCtrl',['$scope', 'loginService',function($scope, 
$ionicModal, $timeout,loginService) { 

这个

.controller('AppCtrl',['$scope','$ionicModal','$timeout','loginService', function($scope, 
$ionicModal, $timeout,loginService) { 
相关问题