角度js自定义服务/工厂无法注入控制器/配置

我试图将自定义工厂注入控制器,但我得到“未知的提供程序:ngResourceProvider < - ngResource < - UserService”错误消息。

当我试图注入自定义筛选器到配置我得到下面的错误消息`无法实例化模块mainApp,由于:错误:[$注射器:unpr] http://errors.angularjs.org/1.5.0-rc.2/$在http:// localhost:8080 / angulartest / scripts / js / angular.min.js:6:421的http:// localhost:8080 / angulartest /脚本中的错误(本地)处注入/ unpr?p0 = ... /hs/angular.min.js:43:7在d(http:// localhost:8080 / angulartest / scripts / js / angular.min.js:40:270)at e(http:// localhost:8080 / (http:// localhost:8080 / angulartest / scripts / js / angular.min.js:41:86)d(http:// localhost:8080 / angulartest / scripts / js / angular.min.js: // http:// localhost:8080 / angulartest / scripts / js / angular.min.js:39:358 at n(http :// localhost:8080 / angulartest / scripts / js / angular.min.js:7:366)at g(http:// localhost:8080 / angulartest / scripts / js / angular.min.js:39:135

我经历了各种博客/视频,我无法解决问题,任何人都可以帮助我。

HTML

    <html >
<head>
<link rel="stylesheet"
    href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet"
    href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
    <link rel="stylesheet" href="./css/custom.css"/>
<script src="./scripts/js/angular.min.js"></script>
<script src="./scripts/js/angular-local-storage.min.js"></script>
<script src="./scripts/js/angular-route.min.js"></script>
<script src="./scripts/js/jquery.scrollbar.js"></script>
<script src="./scripts/js/script.js"></script>
</head>
<body >
    <header>
        <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">Angular Routing Example</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                    <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
                    <li><a href="#contact"><i class="fa fa-comment"></i>
                            Contact</a></li>
                </ul>
            </div>
        </nav>
    </header>

    <!-- MAIN CONTENT AND INJECTED VIEWS -->
    <div id="main" ng-app="myApp" ng-controller="mainController">
        <div ng-view></div>

    </div>
</body>
</html>

用于将服务注入控制器的Java脚本代码

var myApp = angular
        .module('myApp', [ 'ngRoute'])

myApp.factory('UserService',['ngResource', function($resource) {

    return $resource('/login', {}, {
        login : {
            method : 'POST',
            params : {
                'action' : 'authenticate'
            },
            headers : {
                'Content-Type' : 'application/x-www-form-urlencoded'
            }
        },
    });
}]);


myApp.config([ '$routeProvider', '$locationProvider', '$httpProvider','$provide',
        function($routeProvider, $locationProvider, $httpProvider,$provide) {
            $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/login.html',
                controller : 'mainController'
            })

            // route for the about page
            .when('/dashbaord', {
                templateUrl : 'pages/dashboard.html',
                controller : 'dashbaordController'
            })

            $locationProvider.hashPrefix('!');



        } ]);

myApp.controller('mainController',['UserService',function($scope,UserService) {
    function login() {
        alert("hi");
    }
    $scope.login = login;
}]);

myApp.controller('dashbaordController', function($scope) {
    $scope.message = 'Look! I am an about page.';
});

javascript代码注入配置,它是相同的代码下面我已经复制了唯一更改的代码

myApp.config([ '$routeProvider', '$locationProvider', '$httpProvider','$provide','UserService',
        function($routeProvider, $locationProvider, $httpProvider,$provide,UserService) {
            $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/login.html',
                controller : 'mainController'
            })

            // route for the about page
            .when('/dashbaord', {
                templateUrl : 'pages/dashboard.html',
                controller : 'dashbaordController'
            })

            $locationProvider.hashPrefix('!');



        } ]);

如果您使用ngResource,请包含“angular-resource.js”。 您可以在以下链接中找到cdn,Angular ngResource api doc


你只能在.config中注入提供者。 尝试将UserService转换为提供者而不是工厂


UserSerivce是一项服务,所以你不能像提供者那样注入它。 你不需要在app.config中注入服务。 你可以简单地通过注入控制器来使用它。

链接地址: http://www.djcxy.com/p/13607.html

上一篇: angular js Custom Service/factory unable to inject in controller/config

下一篇: Angular get request 415 error