【问题标题】:Split is not a function: Trying to get response from api through GET using ngResource拆分不是函数:尝试使用 ngResource 通过 GET 从 api 获取响应
【发布时间】:2016-12-29 21:43:47
【问题描述】:

我想使用get 方法通过 ngResource 从 API 获得一些响应。

我已经为我的资源创建了一个工厂,但是当我在控制器中使用工厂时,它会抛出错误 URL.split is not a function。我不明白我的代码有什么问题。

var Myapp = angular.module('starter.controllers', ['ngResource'])
    .config(['$resourceProvider', function ($resourceProvider) {
            $resourceProvider.defaults.stripTrailingSlashes = false;
        }]);
Myapp.factory('Users', function ($resource) {
    return $resource('some URL', {}, {
       query: {
          method: 'GET'
       }
    });
});
Myapp.controller('DashCtrl', ['$scope', '$state', 'Users', function ($scope, $state, Users) {
    Users.query().$promise.then(function (data) {
        alert(JSON.stringify(data, null, 4));
    }, function (error) {
        console.log('Error is: ' + JSON.stringify(error, null, 4));
    });

}])

【问题讨论】:

  • 你能分享一下抛出的完整错误吗?
  • 错误:url.split 不是函数
  • 这是我通过开发工具得到的错误。

标签: javascript angularjs ngresource


【解决方案1】:

您注入了 3 个项目('$scope'、'$state'、'Users'),但将 4 个项目发送到函数($scope、$state、$http、Users)。你应该删除$http

Myapp.controller('DashCtrl', ['$scope', '$state', 'Users', function ($scope, $state, Users) {
    Users.query().$promise.then(function (data) {
        alert(JSON.stringify(data, null, 4));
    }, function (error) {
        console.log('Error is: ' + JSON.stringify(error, null, 4));
    });

}])

【讨论】:

  • 这是错误的,我在堆栈溢出上发布了我的问题,我已将其删除,但我仍然收到此错误,我知道我犯了一些愚蠢的错误但无法得到它。请让我摆脱这个.
猜你喜欢
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多