gpt4 book ai didi

angularjs - 为什么 angularjs 的常量不能与工厂一起使用

转载 作者:行者123 更新时间:2023-12-03 00:04:40 25 4
gpt4 key购买 nike

我有常量 mainApp.constant('baseUrl', ' http://localhost:63760 ') ,我需要将它用作我所有工厂的默认基本 url。当我将它注入(inject)到 Controller 中时,它工作正常,但不能与工厂一起工作(当 Controller 调用工厂时),给出未定义的错误。请看我下面的代码。

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
});
}
};
}]);

Controller

    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);
}
});
};
});

最佳答案

您缺少 **'baseUrl',**您应该将其设置为,

mainApp.factory('StudentServices', ['$http', '$rootScope','baseUrl', function ($http, $rootScope, baseUrl) {

关于angularjs - 为什么 angularjs 的常量不能与工厂一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44582365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com