简体   繁体   English

角注入(未知的提供者)

[英]Angular injection (unknown provider)

I am trying to get some json data from my server api. 我正在尝试从服务器API中获取一些json数据。 The url is /contacts. 网址是/ contacts。 I am getting an error about unknown provider but I don't understand what I am doing wrong here. 我收到有关未知提供程序的错误,但我不明白我在做什么错。 I have amended the code again but it seems to happen again. 我再次修改了代码,但似乎又发生了。

 (function() { var app = angular.module('test', ['ngRoute', 'Contacts', '$http']); app.config(function($routeProvider, $locationProvider) { $locationProvider.html5Mode(true); $routeProvider .when("/", { templateUrl: "partials/main.html", controller: "contactsCtrl" }) .otherwise({redirectTo:"/"}); }); app.service('Contacts', function($scope, $http) { this.data = null; this.all = function() { $http.get('/contacts'). success(function(data) { $scope.contacts = data; }) }; }); app.controller('contactsCtrl', function($scope, Contacts) { Contacts.all().query(function(data) { $scope.contacts = data; }); }); }()); 
 <table class="table table-bordered"> <thead> <th>Firstname <input type="search" class="pull-right"></th> </thead> <tbody> <tr ng-repeat="contact in contacts"> <td> <a ng-href="/edit/{{ contact.firstname }}">{{ contact.firstname }}</a> </td> </tr> </tbody> </table> 

Angular modules work by injecting them into your application here. Angular模块通过将它们注入到您的应用程序中来工作。

var app = angular.module('test', ['ngRoute']);

What you need to do would look like this. 您需要做的看起来像这样。

var app = angular.module('test', ['ngRoute','http']);

Here is an example of a working http request I'm currently using in a project of mine. 这是我在我的项目中当前正在使用的http请求的示例。

  var data = $.param({
      workers: JSON.stringify({
          user_name: "name",
          password: "work",
          email: "work@work"
      })
  });

  $http.post("http://ec2-xx-x-xx-xx.compute-1.amazonaws.com/api/user/reg", data).success(function(data, status) {          
      $scope.user = data.user_name;
      $scope.email = data.email;
      $scope.access_token = data.access_token;
  });

You are currently injecting Contacts as a new module here: 当前,您在这里将“联系人”作为新模块注入:

var app = angular.module('test', ['ngRoute', 'Contacts', '$http']);

You don't need this as Contacts is a service and part of the same module. 您不需要此,因为“联系人”是一项服务,并且是同一模块的一部分。 Update this line to: 将此行更新为:

var app = angular.module('test', ['ngRoute', '$http']);

The problem is you're trying to inject $scope into your service . 问题是您正在尝试将$scope注入到service

Scope is the glue between application controller and the view. 范围是应用程序控制器和视图之间的粘合剂。

Please have a look at the documentation . 请看一下文档

Instead of 代替

app.service('Contacts', function($scope, $http) {
  this.all = function() {
    $http.get('/contacts').
     success(function(data) {
       $scope.contacts = data;
    })
  };
});

Try 尝试

app.service('Contacts', function($http) {
  this.all = function() {
    return $http.get('/contacts');
  };
});

Instead of 代替

app.controller('contactsCtrl', function($scope, Contacts) {
  Contacts.all().query(function(data) {
    $scope.contacts = data;
  });
});

Try 尝试

app.controller('contactsCtrl', function($scope, Contacts) {
  Contacts.all().then(function(data) {
    $scope.contacts = data;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM