2017-04-26 37 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) { 
相關問題