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

2024-04-12

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

我对这些文件有问题:

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

身份验证.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/ http://errors.angularjs.org/1.2.17/$injector/unpr?p0=AuthenticationProvider%20%3C-%20Authentication

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


看起来你正在使用两个angular.module在你的应用程序内部authApp & DashboardApp,那么您应该将您的服务提供给DashboardApp通过注入模块authApp进去。

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

假设authApp应该在这样的地方初始化angular.module('authApp',[])

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angularjs 未捕获错误:[$injector:unpr] 的相关文章

随机推荐