1
我有不断mainApp.constant('baseUrl','http://localhost:63760'),我需要使用它作为我所有工厂的默认基础url。当我将它注入到控制器中时,它可以很好地工作,但不能与工厂一起工作(当控制器调用工厂时)会给出未定义的错误。请参阅我的下面的代码。为什么angularjs的常数不能与工厂一起工作
app.js
'use strict';
var mainApp = angular.module("mainApp", ["ui.router", "ui.bootstrap"]);
mainApp.constant('baseUrl', 'http://localhost:63760');
工厂
mainApp.factory('StudentServices', ['$http', '$rootScope', function ($http, $rootScope, baseUrl) {
return {
GetStudentProfileByPIN: function (param) {
alert(baseUrl);
return $http({
url: baseUrl + '/Api/ApiProfile/GetStudentProfile/' + param,
method: 'POST',
async: false
});
}
};
}]);
控制器
mainApp.controller('loginController', function ($scope, $rootScope, $state, $window, StudentServices) {
$scope.user = {};
$scope.login = function() {
StudentServices.GetStudentProfileByPIN($scope.PIN).then(function (response) {
if (response.data.Student != null) {
$window.localStorage.setItem('Student', angular.toJson(response.data));
$window.location.href = "/Profile/#/home";
}
else {
alert(response.data.Message);
}
});
};
});