【问题标题】:Angular $resource returns TypeErrorAngular $resource 返回 TypeError
【发布时间】:2015-09-12 22:13:02
【问题描述】:

您好,我正在尝试使用 ngResource 从我的 api 获取 url,并且我有一个返回资源的服务。但是,当我在我的控制器中调用它时,会出现无法读取未定义的属性“查询”的错误。

为什么会这样?

这是我的服务:

(function() {

  var app = angular.module('test');

  app.service('ContactResource', ['$resource', function($resource) {

      return $resource('/contacts/:firstname', {firstname: '@firstname'},
        {
          'update': {method: 'PUT'}
        }
      );
  }]);

}());

这是我的控制器

(function() {

  var app = angular.module('test');

  app.controller('contactsCtrl', ['ContactResource', function($scope, Contacts, ContactResource) {
    $scope.contacts = ContactResource.query();
  }]);

}());

这是我的 HTML

<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="/{{ contact.firstname }}">{{ contact.firstname }}</a>
      </td>
    </tr>
  </tbody>
</table>

【问题讨论】:

    标签: angularjs resources


    【解决方案1】:

    这是你的问题:

    ['ContactResource', function($scope, Contacts, ContactResource)
    

    您还需要将$scopeContacts 注入到数组中,如下所示:

    ['$scope', 'Contacts', 'ContactResource', function($scope, Contacts, ContactResource)
    

    否则 Angular 会认为 'ContactResource'$scope 相同。

    【讨论】:

    • 非常感谢。那解决了它。很好的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多