Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS:返回零结果的服务查询_Javascript_Angularjs_Angularjs Scope_Angularjs Service_Angularjs Routing - Fatal编程技术网

Javascript AngularJS:返回零结果的服务查询

Javascript AngularJS:返回零结果的服务查询,javascript,angularjs,angularjs-scope,angularjs-service,angularjs-routing,Javascript,Angularjs,Angularjs Scope,Angularjs Service,Angularjs Routing,我的app.js看起来像 var app = angular.module('pennytracker', [ '$strap.directives', 'ngCookies', 'categoryServices' ]); app.config(function($routeProvider) { console.log('configuring routes'); $routeProvider .when('/summary', { templateUrl: '

我的
app.js
看起来像

var app = angular.module('pennytracker', [
  '$strap.directives',
  'ngCookies',
  'categoryServices'
]);

app.config(function($routeProvider) {
  console.log('configuring routes');
  $routeProvider
    .when('/summary', { templateUrl: '../static/partials/summary.html'})
    .when('/transactions', { templateUrl: '../static/partials/transaction.html', controller: 'AddTransactionController' })
});
function AddTransactionController($scope, $http, $cookieStore, Category) {
 // some work here
  $scope.category = Category.query();
  console.log('all categories - ', $scope.category.length);
}
而我的
app/js/services/categories.js
看起来像

angular.module('categoryServices', ['ngResource']).
  factory('Category', function($resource){
    return $resource(
      '/categories/:categoryId',
      {categoryId: '@uuid'}
    );
  });
我有一条路线

  .when('/transactions', { templateUrl: '../static/partials/transaction.html', controller: 'AddTransactionController' })
我的控制器
app/js/controllers/transactionController.js
看起来像

var app = angular.module('pennytracker', [
  '$strap.directives',
  'ngCookies',
  'categoryServices'
]);

app.config(function($routeProvider) {
  console.log('configuring routes');
  $routeProvider
    .when('/summary', { templateUrl: '../static/partials/summary.html'})
    .when('/transactions', { templateUrl: '../static/partials/transaction.html', controller: 'AddTransactionController' })
});
function AddTransactionController($scope, $http, $cookieStore, Category) {
 // some work here
  $scope.category = Category.query();
  console.log('all categories - ', $scope.category.length);
}
当我运行我的应用程序时,我看到console.log为

all categories -  0 
我在这里做错了什么?

Category.query()
是异步的。它立即返回一个空数组,并在稍后响应到达时添加请求的结果-来自:

重要的是要认识到调用$resource对象方法 立即返回空引用(对象或数组,具体取决于 伊萨雷)。从服务器返回数据后,现有的 引用用实际数据填充。这是一个有用的技巧 因为资源通常分配给一个模型,然后 由视图渲染。如果对象为空,则不会进行渲染, 一旦数据从服务器到达,对象就会被填充 随着数据和视图自动重新呈现自身,显示 新数据

如果需要在控制器中访问结果,应在回调函数中执行以下操作:

$scope.category=category.query(函数(){
log('all categories-',$scope.categority.length);
});
geez:(,我怎么会错过文档中的那部分。非常感谢@joakimbi,它现在可以工作了