【问题标题】:Variable not defined : AngularJS未定义变量:AngularJS
【发布时间】:2014-04-23 00:45:58
【问题描述】:

我对 Angular JS 还很陌生,但我正在开发一项服务来从 JSON 中获取信息:

这是服务代码:

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

staffServices.factory('Staff', function($resource){
return $resource('/api/staff/1', {}, {
 query: {method:'GET', params: {}, isArray:false}
});
});

控制器

staffApp.controller('StaffCtrl', function($scope, Staff) {
Staff.query(function(data) {
      console.log(data);
      $scope.staff = data;
      console.log(staff);
  });

但是,当我运行应用程序时,我可以将“数据”视为对象,但无法将其分配给范围变量,我收到以下“错误:未定义人员” .

感谢您的所有回答!

【问题讨论】:

  • 出现错误的行是什么?
  • 应该是console.log($scope.staff);
  • @klode 把它放在一个正式的答案中;我认为你成功了。

标签: javascript angularjs service controller angularjs-scope


【解决方案1】:

代替

console.log(staff); // this prints the var staff, which is undefined

应该是

console.log($scope.staff); // this prints the staff property of $scope

【讨论】:

    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2011-06-11
    • 2014-05-27
    相关资源
    最近更新 更多