【问题标题】:Unknow Provider with AngularJSAngularJS 的未知提供者
【发布时间】:2015-06-13 19:24:16
【问题描述】:

我正在尝试使用 angularjs 请求 jason 文件。我正在使用 yeoman。这是我的 3 个文件。

在视图文件夹中:main.html

<div class="jumbotron">


Choose A Category1
 <div class="wrap" ng-controller="MainCtrl">
<select ng-model="selectedValue" ng-change="loadData()" >
  <option value="1">Category 1</option>
  <option value="2">Category 2</option>
  <option value="3">Category 3</option>
  <option value="4">Category 4</option>
  <option value="5">Category 5</option>
</select>



<div ng-show="selectedValue != null" class="main">
  <center><h2>Results For Category  {{ selectedValue }}</h2></center> <br><br>

</div>


<div  class="main"  >
  <ul class="cloudlist">
    <li class="service" ng-repeat="item in services" ng-click="select(item)">
      <div class="info">

        <h3>{{item.service_name}}</h3>
        <b>{{item.status_page}}</b><br>
        <b>Is Billed : {{item.is_billed.billing_term._identifier}}</b>


      </div>
    </li>
  </ul>
  <br>
</div>

</div>

在控制器文件夹中:main.js

'use strict';


angular.module('frontendApp')
.controller('MainCtrl', ['$scope', '$http','Services', function ($scope, $http, Services) {
    $scope.loadData = function () {
        $scope.services = Services.query();
    };






$scope.select = function (item) {
       // do something with the item here when the service is clicked  
  };


}]);

在服务文件夹中:services.js

'use strict';

angular.module('frontendApp').factory('Services', function($resource) {
  return $resource('/services/:serviceId', {
    serviceId: '@_id'
  }, {});
});

我收到一个错误

错误:[$injector:unpr] 未知提供者:ServicesProvider http://errors.angularjs.org/1.3.15/$injector/unpr?p0=ServicesProvider%20%3C-%20Services%20%3C-%20MainCtrl minErr/http://localhost:9000

解决方案: 在使用 yeoman 时,忘记执行命令yo angular:service Services

【问题讨论】:

  • 你是在加载services.js之前加载main.js吗?
  • 这是漏洞代码,我没有加载任何东西
  • 您必须以某种方式引用脚本,或者通过&lt;script src=".."&gt; 分别引用每个脚本,或者通过将它们全部组合到一个文件的服务。你如何引用它们?
  • 我用的是yeoman,我以为他关心这一切
  • @user567 任何框架怎么会知道我需要自动加载 js 文件..而不提示框架..

标签: angularjs angularjs-service


【解决方案1】:

在你的 main.js 中试试这个代码

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

module.factory('MyServices', ['$resource', function($resource) {
    return $resource('/services/:serviceId', {
        serviceId: '@_id'
    }, {});
}]);

module.controller('MainCtrl', ['$scope', '$http','MyServices', function ($scope, $http, MyServices) {
    $scope.loadData = function () {
        $scope.services = MyServices.query();
    };
}]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多