为什么angularjs一直不与工厂合作

时间:2017-06-16 06:35:01

标签: angularjs

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

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

2 个答案:

答案 0 :(得分:4)

你缺少** var jsonObj = { "Elements": [{ "LowerTolerance": 1.4, "Name": "abc", "ReferenceValue": 1.5, "UpperTolerance": 1.6, "Valid": false, "Value": 1.8 }, { "LowerTolerance": 20, "Name": "def", "ReferenceValue": 21.5, "UpperTolerance": 23, "Valid": true, "Value": 22.8 }, { "LowerTolerance": 4.5, "Name": "ghi", "ReferenceValue": 5, "UpperTolerance": 5.5, "Valid": false, "Value": 4 }], "Kamera": "c1" }; let lowertolerance = []; let name = []; let referencevalue = []; let uppertolerance = []; let valid = []; let value = []; for (let item of jsonObj.Elements) { lowertolerance.push(item.LowerTolerance); name.push(item.Name); referencevalue.push(item.ReferenceValue); uppertolerance.push(item.UpperTolerance); valid.push(item.Valid); value.push(item.Value); } console.log("lowertolerance :", lowertolerance); console.log("name :", name); console.log("referencevalue :", referencevalue); console.log("uppertolerance :", uppertolerance); console.log("valid :", valid); console.log("value :", value); **你应该把它作为,

'baseUrl',

答案 1 :(得分:1)

也注入字符串baseUrl

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