【问题标题】:$resource and $promise returning undefined$resource 和 $promise 返回 undefined
【发布时间】:2014-05-12 17:12:01
【问题描述】:

我收到以下错误:

TypeError: Cannot call method 'then' of undefined

关于此代码:

App.controller('MainCtrl', ['$scope', 'Main', 'MainFilter', function($scope, Main, MainFilter) {
    $scope.scope_variable = Main.query().$promise.then( function(result) { MainFilter.do_something(result) } );
}]);

App.factory('Main', ['$resource', function($resource) {
    return $resource('/main_resource.json')
}]);

我一直在尝试遵循这些在控制器中使用 promise 的其他示例(我没有使用 $routeProvider),但似乎没有任何效果。

AngularJS using $resource service. Promise is not resolved by GET request

AngularJS resource promise

【问题讨论】:

  • 你在哪里定义$promise
  • Main.query({}, function(data) { console.log(data) })
  • 我假设 $promise 是在 $resource 中定义的...
  • @tymeJV 控制台记录数据返回正确的资源数组。
  • 为什么不使用我当时提供的代码的 sn-p 呢?应该可以访问返回的数据,一切都很好

标签: javascript angularjs asynchronous ngresource


【解决方案1】:

@tymeJV 提供了这个答案:

$scope.scope_variable = Main.query({}, function(result) { MainFilter.do_something(result) } );

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2014-06-29
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2022-12-07
    相关资源
    最近更新 更多