AngularJS Error: $injector:unpr Unknown Provider

后端 未结 13 945
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 08:10

I\'m trying to build my own service by following the example in the documentation for the factory methodology. I think I\'ve done something wrong however because I continue

13条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 09:04

    Spent a few hours trying to solve the same. This is how I did it:

    app.js:

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

    CustomServices is a new module I created and placed in a separate file called services.js

    _Layout.cshtml:

    
    
    

    services.js:

    var app = angular.module('CustomServices', []); 
    app.factory( 'GetPeopleList', ['$http', '$log','$q', function ( $http, $log, $q )
    {
        //Your code here
    }
    

    app.js

    myApp.controller( 'mainController', ['$scope', '$http', '$route', '$routeParams', '$location', 'GetPeopleList', function ( $scope, $http, $route, $routeParams, $location, GetPeopleList )
    

    You have to bind your service to your new module in the services.js file AND of course you have to use that new module in the creation of your main app module (app.js) AND also declare the use of the service in the controller you want to use it in.

提交回复
热议问题