locked
Error when trying to inject service in angular controller - Error: [$injector:unpr] Unknown provider: RRS feed

  • Question

  • User2058796294 posted

    Hi,

    I am new to `AngularJs`.
    I am trying to consume Web API using AngularJs but getting struck angular side which is hard for me to figure out.

    I created HTML, controller and service. Everything seems ok to me but when running the app i get the injection error.


    html

    <html >
    <head>
    <title>Motors </title>
    <script src="/Scripts/angular.js"></script>
    <script src="/Scripts/angular-route.js"></script>
    <script src="/View/motorController.js"></script>
    </head>
    <body ng-app="myApp" ng-controller="motorController">
    <div>
    <table class="table">
    <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Country</th>
    </tr>
    <tr ng-repeat="m in motors">
    <td>{{m.Id}}</td>
    <td>{{m.Name}}</td>
    <td>{{m.Country}}</td>
    </tr>
    </table>
    </div>
    </body>
    </html>




    **AngularJs controller**

    var module = angular.module('myApp', [])
    .controller('motorController', ['$scope', '$motorService',function ($scope, motorService) {
    
    getMotors();
    function getMotors() {
    motorService.GetAllMotors()
    .success(function (motors) {
    $scope.motors = motors;
    })
    .error(function (error) {
    $scope.status = 'Unable to load motor data: ' + error.message;
    });
    }
    }]);




    **angular service**

    motorApp.factory('motorService', function ($http) {
    var urlBase = 'http://localhost:40738/api';
    var motorService = {};
    
    motorService.GetAllMotors = function () {
    return $http.get(urlBase + '/GetAllMotors');
    };
    
    return motorService;
    });



    Error i am getting on chrmoe browser console

    Error: [$injector:unpr] Unknown provider: $motorServiceProvider <- $motorService <- motorController

    Saturday, September 19, 2015 2:35 AM

Answers

  • User61956409 posted

    Hi MuhammadSohail,

    Error: [$injector:unpr] Unknown provider: $motorServiceProvider <- $motorService <- motorController

    The following link explained about this issue, please refer to it.

    https://docs.angularjs.org/error/$injector/unpr

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2015 2:02 AM