Angularjs未捕获错误:[$ injector:unpr]

时间:2015-06-28 10:20:28

标签: javascript angularjs angularjs-module

我正在使用java开发购物网站,我正在使用angurajs。

我对这个文件有问题:

DashboardControll.js

    'use strict';
var app = angular.module("DashboardApp", []);

app.controller("DashboardCtrl", function($scope, $http, Authentication) {
    $http.get("/SalonNamestaja/namestaj")
    .success(function(response) {
        $scope.namestaji = response;
    });


        $http.get("/SalonNamestaja/ActiveUser")
        .success(function(response) {
            //console.log(response);


            $(".navbar-brand").empty();
            $(".navbar-brand").append("Dobrodosli " + response.name);

            $scope.activeUser = response;
        });

        console.log(Authentication.getUser());
});

app.run(function(Authentication) {
    Authentication.requestUser();
});

Authentication.js

'use strict';

angular.module('authApp').service('Authentication', function Authentication($q, $http) {
    var authenticatedUser = null;

    return {
        requestUser: function() {
            var deferred = $q.defer();

            $http.get("/SalonNamestaja/ActiveUser")
            .success(function(user) {
                console.log(user);
                authenticatedUser = user;

                deferred.resolve(user);
            }).error(function(error) {
                deferred.reject(error);
            });

            return deferred.promise;
        },

        getUser: function() {
            return authenticatedUser;
        },

        exists: function() {
            return authenticatedUser != null;
        }
    }
})

当我在浏览器中加载页面时,我收到错误:

  

未捕获错误:[$ injector:unpr]   http://errors.angularjs.org/1.2.17/ $注射器/ unpr?P0 =的AuthenticationProvider%20%3 C-%20Authentication

请有人帮我解决这个错误。

1 个答案:

答案 0 :(得分:2)

您似乎在应用程序angular.module内使用了两个authApp& DashboardApp,那么您应该通过向DashboardApp注入authApp来提供服务{/ 1}}。

var app = angular.module("DashboardApp", ["authApp"]);

假设authApp应该像angular.module('authApp',[])

那样初始化