2016-07-20 38 views
0

在我的angularJS應用程序中,我已經配置了我的應用程序,如下所示。我已經定義了一個模塊服務來定義我所有的工廠方法。AngularJS App中的未知服務提供商

angular.module("services", []); 
var app = angular 
    .module('myApp', [ 
    'services' 
    ]); 

在另一個文件中,我已經在服務模塊定義的工廠,但是當我運行的應用程序,我得到以下錯誤:

Unknown provider: myServicesProvider <- myServices <- myCtrl

angular.module('services').factory('myServices', function ($http) { 
    return { 
    createPet(pet) { 
     return $http.post('/pets', pet); 
    } 

    }; 
}); 

angular.module('myApp') 
    .controller('myCtrl', myServices) { 

    }); 

我查了很多職位的計算器我想我已經以正確的方式完成了定義。任何人都有線索?

回答

0

您已經在工廠放錯地方的括號,

這裏是工作代碼:

angular.module("services", []); 
var app = angular 
    .module('myApp', [ 
    'services' 
    ]); 

app.controller('mainController', function($scope, myServices){ 
    $scope.items = myServices.query(); 
    $scope.bill = {}; 
    $scope.totalBeforeDiscount = {}; 
    $scope.totalPrice = function(){ 
     var total = 0; 
     for (var i = 0; i <= $scope.items.length - 1; i++) { 
      total += $scope.items[i].price * $scope.items[i].quantity; 
     } 
     $scope.totalBeforeDiscount =total; 
    } 
}) 

angular.module('services').factory('myServices', function ($http) { 
var items = {}; 
    return { 
    query: function() { 
     return [{ 
     title: 'Paint pots', 
     quantity: 8, 
     price: 3.95 
     }, { 
     title: 'Polka dots', 
     quantity: 17, 
     price: 12.95 
     }, { 
     title: 'Pebbles', 
     quantity: 5, 
     price: 6.95 
     }]; 
    } 
    }  

}); 

工作Application

+0

對不起它是在複製粘貼一個錯字。我用更多的細節更新了這個問題。我也有一個使用該服務的控制器,然後我得到上面的其他錯誤。 – sansari

+1

@sansari檢查plunker應用程序,它的工作原理 – Sajeetharan

+0

謝謝,我發現我沒有在index.html中添加myService.js!當項目越來越大時,會有更多的js文件。我如何管理它們而不用在html.index中添加它們?再次感謝 – sansari

相關問題