【问题标题】:AngularJS : How to inject custom service in controller?AngularJS:如何在控制器中注入自定义服务?
【发布时间】:2014-10-08 18:08:32
【问题描述】:

我正在使用 AngularJS。我有以下页面:

<div ng-controller="UserController">
<h4>Create an account</h4>
<form class="form-horizontal" role="form">

  <div class="form-group">
    <label for="inputLastName" class="col-sm-2 control-label">Last name</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="inputLastName" placeholder="Enter last name" ng-model="user.lastname">
    </div>
  </div>

    <div class="form-group">
    <label for="inputFirstName" class="col-sm-2 control-label">First name</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="inputFirstName" placeholder="Enter first name" ng-model="user.firstname">
    </div>
  </div>
  <div class="form-group">
    <label for="inputEmail" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail" placeholder="Email" ng-model="user.email">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button ng-click="createUser()" class="btn btn-primary">Create account</button>
    </div>
  </div>
</form>
</div>

我有用户控制器:

app.controller('UserController', function ($rootScope, $scope, $location, UserSevice) {

    $scope.users = ContactService.list();

    $scope.createUser = function () {
        UserService.saveUser($scope.user);
        $scope.user = {};
    }

    $scope.deleteUser = function (id) {
        UserService.deleteUser(id);
        if ($scope.user.id == id) $scope.user= {};
    }

    $scope.editUser = function (id) {
        $scope.user= angular.copy(UserService.get(id));
    }
 });

这里是用户服务:

app.service('UserServices', function ($http, $location) { 
   // some logic
}

但我认为我必须提供我的服务提供商但我不知道该怎么做

Error: [$injector:unpr] Unknown provider: UserSeviceProvider <- UserSevice
http://errors.angularjs.org/1.2.25/$injector/unpr?p0=UserSeviceProvider%20%3C-%20UserSevice
    at http://localhost:8080/app-web/js/lib/angular.js:78:12
    at http://localhost:8080/app-web/js/lib/angular.js:3802:19
    at Object.getService [as get] (http://localhost:8080/app-web/js/lib/angular.js:3930:39)
    at http://localhost:8080/app-web/js/lib/angular.js:3807:45
    at getService (http://localhost:8080/app-web/js/lib/angular.js:3930:39)
    at invoke (http://localhost:8080/app-web/js/lib/angular.js:3957:13)
    at Object.instantiate (http://localhost:8080/app-web/js/lib/angular.js:3977:23)
    at http://localhost:8080/app-web/js/lib/angular.js:7281:28
    at http://localhost:8080/app-web/js/lib/angular.js:6670:34
    at forEach (http://localhost:8080/app-web/js/lib/angular.js:332:20) <div ng-view="" class="ng-scope"> 

【问题讨论】:

  • 这看起来像是一个错字。始终建议您在采取任何进一步措施之前非常仔细地查看您的错误消息。如果您仔细查看错误消息,您可能只为自己节省了 10 分钟。

标签: angularjs angularjs-directive


【解决方案1】:

你有一个错字:

app.service('UserServices', function ($http, $location) { 

但是你注入的是UserService(实际上是UserSevice——我猜有几个错别字)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2013-05-28
    • 1970-01-01
    • 2014-09-09
    • 2012-05-20
    相关资源
    最近更新 更多