简体   繁体   English

为什么我在 AngularJS 中收到错误“ReferenceError:类别未定义”?

[英]Why am I getting an error, "ReferenceError: categories is not defined" in AngularJS?

In my understanding, $scope.categories is already defined.在我的理解中, $scope.categories 已经定义好了。 Then why am I getting this error and not able to access data from the Json file?那为什么我会收到此错误并且无法访问 Json 文件中的数据?

Here is my controller:这是我的控制器:

(function(){

    app.controller('productsCtrl', ['$scope','$cookies', '$http', function($scope,$cookies,$http){

        $http.get("controllers/data.json").then(function (response) {
            $scope.categories = response.data;
        }); 

        $scope.specials = [categories[0].laptops[1], categories[1].accessories[0]];

    }]);

})(); 

Here is my Json file:这是我的 Json 文件:

[
  {
    "laptops": [     
      {
        "name": "Asus Laptop",
        "price": 300
      },
      {
        "name": "HP Notebook",
        "price": 200
      }
    ]
  },
  {
    "accessories": [
      {
        "name": "WD Hard Drive",
        "price": 100
      },
      {
        "name": "WD Blue SSD",
        "price": 700
      }
    ]
  }
] 

You have assigned response data into $scope.categories , you need to use $scope.categories instead of categories and also you should add into $scope.specials once http call completed like您已将响应数据分配到$scope.categories ,您需要使用$scope.categories而不是categories并且一旦 http 调用完成,您还应该添加到$scope.specials

(function(){

    app.controller('productsCtrl', ['$scope','$cookies', '$http', function($scope,$cookies,$http){

        $scope.specials = [];
        $http.get("controllers/data.json").then(function (response) {
            $scope.categories = response.data;
            $scope.specials.push($scope.categories[0].laptops[1]);
            $scope.specials.push($scope.categories[1]. accessories[0]);
        }); 

        

    }]);

})(); 

There's actually a few issues here.这里其实有几个问题。 First is you never define a variable named categories, you have a property of the $scope object named that, so you need to access $scope.categories or the response.data setting it directly.首先是你从来没有定义一个名为类别的变量,你有一个名为 $scope 对象的属性,所以你需要访问$scope.categories或直接设置它的response.data

Second, you have a race condition issue.其次,你有一个竞争条件问题。 You are trying to access the values of categories outside the promise, meaning potentially before the get request has returned any data.您正在尝试访问承诺之外的类别值,这意味着可能在 get 请求返回任何数据之前。 When you use get().then() like you are, the code after the request doesn't wait for the request to finish before it runs, so whatever is faster runs first.当你像你一样使用get().then() ,请求之后的代码不会等待请求完成后再运行,所以先运行速度更快的代码。 Because one of the two operations running is accessing an external endpoint and the other is local javascript code, the flow is almost guaranteed to be this:因为正在运行的两个操作之一是访问外部端点,另一个是本地 javascript 代码,所以几乎可以保证流程是这样的:

  1. send get request to "controllers/data.json"向“controllers/data.json”发送获取请求
  2. set $scope.specials - causing your undefined error设置 $scope.specials - 导致您未定义的错误
  3. set $scope.categories when get request promise resolves当获取请求承诺解决时设置 $scope.categories

You need to access the categories inside the promise to guarantee that it actually has been defined at the point you are trying to access it:您需要访问 promise 中的类别,以确保它实际上已在您尝试访问它时定义:

$http.get("controllers/data.json").then(function (response) {
    $scope.categories = response.data;
    $scope.specials = [$scope.categories[0].laptops[1], $scope.categories[1].accessories[0]];
}); 

It's also generally a bad idea to hard code indexes like this, if the data changes you run into a possible index out of bounds error.像这样对索引进行硬编码通常也是一个坏主意,如果数据发生变化,您可能会遇到索引越界错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现“ Uncaught ReferenceError:未定义userId”错误? - Why am I getting “Uncaught ReferenceError: userId is not defined” error? 为什么我收到ReferenceError:框未定义错误 - Why am I getting ReferenceError: boxes is not defined error 为什么我会收到此错误 ReferenceError: channel is not defined - Why am i getting this error ReferenceError: channel is not defined 为什么我得到“ReferenceError:getElementById未定义”? - Why am I getting “ReferenceError: getElementById is not defined”? 为什么收到“ ReferenceError:未定义$ cacheFactory” - Why am I getting “ReferenceError: $cacheFactory is not defined” 为什么我得到一个ReferenceError:未定义 - Why am I getting a ReferenceError: not defined 我为什么会收到“ Uncaught ReferenceError:未定义反应” - Why am I getting “Uncaught ReferenceError: React not defined” 为什么我得到:Uncaught ReferenceError: response is not defined - why am i getting: Uncaught ReferenceError: response is not defined Vue/Vuetify:为什么我会收到一个 ReferenceError:变量未定义? - Vue/Vuetify: why am I getting a ReferenceError: variable is not defined? 为什么我得到 Uncaught ReferenceError: React is not defined? - Why am I getting Uncaught ReferenceError: React is not defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM