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

标签 angularjs

我有常量 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/

相关文章:

javascript - AngularJS 与 ui-router,状态变得困惑

javascript - Angularjs 在函数之外获取对象值

javascript - AngularJS 类型错误 : $resource is not a function

javascript - AngularJS - 基于 API 的实时搜索不会随着 ng-model 的变化而更新

AngularJS ng-include 不工作

javascript - 如何从 AngularJS 列表中删除用户?

javascript - 根据属性创建唯一的数组

java - 使用 ionic 构建 PhoneGap 应用程序时出现构建错误

javascript - $http put v create - 每个(JavaScript)中从 .success() 返回的数据对象之间有什么区别

javascript - ng-init : Error: $parse:Lexer Error: Unexpected next character, AngularJS 中的特殊字符