如何正确指定和引用提供程序

时间:2014-04-14 03:58:08

标签: angularjs

我正在尝试将Angular集成到我的MVC 5应用程序中。 到目前为止,我将模块定义,服务定义和控制器定义都放在一个文件中:

var frontEndApp = angular.module('frontEndApp', []);

frontEndApp.factory('customersRepository', function ($resource) {
    return {
        get: function () {
            return $resource('http://localhost/ServiceHub.Web/api/Customers/').query();
        }
    }
});

 function UserRequestCtrl($scope,  customersRepository) {
     $scope.ShortName;
     $scope.FirstName;
     $scope.LastName;
     $scope.UserEmail;
     $scope.UserOrgRole;
     $scope.UserPhoneNumber;

     $scope.customerInstances = customersRepository.get();

     var list = [];

     $scope.checkit = function () {

         for (var p in $scope.customerInstances) {
             if ($scope.customerInstances[p].checked) {
                 list.push($scope.customerInstances[p].CustomerName);

                 console.log("selected customer:  " + $scope.customerInstances[p].CustomerName);
             }
         } return list;
     }

     $scope.saveUser = function () {

         var UserDetail =
             {
                 shortName: $scope.ShortName,
                 firstName: $scope.FirstName,
                 lastName: $scope.LastName,
                 userEmail: $scope.UserEmail,
                 userOrgRole: $scope.UserOrgRole,
                 userPhoneNumber: $scope.UserPhoneNumber,
                 isAdministrator: $scope.UserCitizenship,
                 customerInstances:
                list.filter(function (itm, i, a) {
                    return i == a.indexOf(itm);
                }).toString(),
             }
         console.log("User Detail:  short name  " + $scope.ShortName + "  first name  " + $scope.FirstName + "   last name" + $scope.LastName + "   email  " +
                    $scope.UserEmail + "   org role  " + $scope.UserOrgRole + "   phone  " + $scope.UserPhoneNumber + "   citizenship  " + $scope.UserCitizenship);
         console.log(list.filter(function (itm, i, a) {
             return i == a.indexOf(itm);
         }).toString());


     }
 };

当我运行时,我收到此错误:

Error: [$injector:unpr] Unknown provider: customersRepositoryProvider <- customersRepository
http://errors.angularjs.org/1.2.16/$injector/unpr?p0=customersRepositoryProvider%20%3C-%20customersRepository

对我来说,定义和引用这3个部分以使它们协同工作的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

customersRepository绑定到您的角度模块,因此您只需要将控制器挂钩到同一模块,如下所示:

frontEndApp.controller('UserRequestCtrl', function($scope,  customersRepository) {
...
});