Angular JS依赖错误

时间:2016-01-24 21:21:07

标签: angularjs requirejs angular-ui-router

收到错误

  

错误:[$ injector:unpr]未知提供者:studstudentServiceProvider< - studstudentService< - studentCtrl

我的main.js看起来像这样:

require.config({
    //baseurl: '/Scripts/',
     paths: {
    'jquery': 'Scripts/jquery-2.2.0.min',
    'angular': 'Scripts/angular',
    'ngStorage': 'Scripts/ngStorage',
    'ngCookies': 'Scripts/angular-cookies',
    'ui-router': 'Scripts/angular-ui-router',

    'bootstrap': 'Scripts/bootstrap',
    'service': 'services/service',
    'studentCtrl': 'controllers/studentCtrl',
    //'homeCtrl': "controllers/homeCtrl",
    //'accountCtrl': "controllers/accountCtrl",
    //'filter': "filters/filter",
    },
    shim: {
    ngStorage: {
        deps: ['angular'],
        exports: 'angular'
     },
     ngCookies: {
        deps: ['angular'],
        exports: 'angular'
      },
    'ui-router': {
        deps: ['angular'],
        exports: 'angular'
      },
      angular: {
        exports: 'angular'
      },
     bootstrap: {
        deps: ['jquery']
     }
     },
     deps: ['app']
     });

   require([
      "app",

    "bootstrap",
     "studentCtrl"

     ],

   function (app) {
    //bootstrapping app
    app.init();
    }
    );

我的app.js看起来像这样:

define(['ui-router', 'ngStorage', 'ngCookies'], function () {

    //defining angularjs module
    var app = angular.module("app", ['ui.router', 'ngCookies', 'ngStorage']);

    //global service
    app.constant("utility",
        {
            baseAddress: "http://localhost:15985/api/"
        });

    //manual bootstrap
    app.init = function () {
        angular.bootstrap(document, ['app']);
    };

    //defining routes
    app.config(function ($stateProvider, $urlRouterProvider) {

        $urlRouterProvider.otherwise("/");

        $stateProvider
            .state("home", {
                url: "/",
                templateUrl: 'views/home/home.html',
                controller: 'homeCtrl'
            })
        .state("students", {
            url: "/students",
            templateUrl: 'views/students/students.html',
            controller: 'studentCtrl'
        });
    });
    return app;
});

我的studentCtrl.js看起来像这样:

define(['app', 'service'], function (app) {
    app.controller("studentCtrl", function ($scope, studstudentService) {
        $scope.students = [];
        $scope.student = null;
        alert('hi');
        //get User
        $scope.get = function () {
            $scope.student = this.student;

        };

        // initialize your students data
        (function () {
            studentService.getStudentsList().success(function (data) {
                $scope.students = data;
            }).error(function (data) {
                $scope.error = "An Error has occured while Loading students! " + data.ExceptionMessage;
            });
        })();


    });
});

我的service.js看起来像这样:

define(['app'], function (app) {
    //defining service using factory method
    app.factory('studentService', function ($http, utility) {
        var serviceurl = utility.baseAddress + "student/";
        return {
            getStudentsList: function () {
                var url = serviceurl + "GetStudents";
                return $http.get(url);
            }

        };
    });
});

1 个答案:

答案 0 :(得分:0)

相当简单

您正在注入studstudentService,但服务名称为studentService且拼写不同