从angularjs中的外部文件加载数组

时间:2023-01-24 09:50:44

it's my first time here on stack and I hope someone can help me because I'm stuck!

这是我第一次在这里堆叠,我希望有人可以帮助我,因为我被卡住了!

I have a script made in AngularJS that creates multiples icons extracting the data from a simple array. Every icon is associated to a youtube video.

我在AngularJS中创建了一个脚本,它创建了从简单数组中提取数据的多个图标。每个图标都与YouTube视频相关联。

The array and the function is simple:

数组和函数很简单:

app.controller('videolist', function ($scope, $http) {
$scope.videos = ['q_664lrmyGE','q_664lrmyGE','dWuEVSCw8B8','3Wm3G8s8bxk','_ppuCZR8Mkw','9gN_cmK9TUc','s7lVGhTPQAY','_rNmmHXEdTc','blr-qDffIq0','_70M4lkLKPk','wjB9JtTU7SU','BGh1xc-O0WA','uxEBK9q686c','ToO-tS-X2U4','AKrmrbCTNxc'];

$scope.icon = [];
  // add youtube videos
  for (i=0; i<$scope.videos.length; i++) {
    $scope.icon.push({id: $scope.videos[i], thumb: 'mqdefault'});

  }
});

Now I would like to load the array from an external JSON file.

现在我想从外部JSON文件加载数组。

I've tried in any way possible, but didn't work. How I can do it? And how should the JSON file be composed? In every example I've see there are at least two or three values that makes one record, or a pair made with "label":"value".

我尝试过任何可能的方法,但没有奏效。我怎么能这样做?如何组成JSON文件?在每个例子中,我都看到至少有两个或三个值构成一个记录,或者一对用“label”:“value”。

Thank you so much!!

非常感谢!!

2 个解决方案

#1


0  

An array alone is not valid JSON so you'll have to embed the array inside of a parent JSON object like so:

单独一个数组是无效的JSON所以你必须将数组嵌入到父JSON对象中,如下所示:

var videos = {'videoArray':['q_664lrmyGE','q_664lrmyGE','dWuEVSCw8B8',...]}

And then you'd access the array by referencing videos.videoArray

然后你通过引用videos.videoArray来访问数组

#2


0  

JSon Format

Disperse is right with the composition of the json format ( $scope.videos = {'keyToArray':['foo','bar','baz',]} ). Also, while I am somewhat new as well, I think disperse has a point using just var instead of $scope. because you don't need it to be in scope if you are just creating a variable and then crunching it later down in your function.

分散是正确的json格式的组成($ scope.videos = {'keyToArray':['foo','bar','baz',]})。此外,虽然我也有点新,但我认为驱散只使用var而不是$ scope。因为如果你只是创建一个变量然后稍后在你的函数中进行处理,你就不需要它在范围内。

Load data from file

Judging by your unused $http service, I think you may be asking about how to get the data from a .json file you have stored? For that, do something along the following:

根据你未使用的$ http服务判断,我想你可能会问如何从你存储的.json文件中获取数据?为此,请执行以下操作:

$http.get("video.json").then(function (response) {
    $scope.videos = response.data;
});

Hope this helps!

希望这可以帮助!

#1


0  

An array alone is not valid JSON so you'll have to embed the array inside of a parent JSON object like so:

单独一个数组是无效的JSON所以你必须将数组嵌入到父JSON对象中,如下所示:

var videos = {'videoArray':['q_664lrmyGE','q_664lrmyGE','dWuEVSCw8B8',...]}

And then you'd access the array by referencing videos.videoArray

然后你通过引用videos.videoArray来访问数组

#2


0  

JSon Format

Disperse is right with the composition of the json format ( $scope.videos = {'keyToArray':['foo','bar','baz',]} ). Also, while I am somewhat new as well, I think disperse has a point using just var instead of $scope. because you don't need it to be in scope if you are just creating a variable and then crunching it later down in your function.

分散是正确的json格式的组成($ scope.videos = {'keyToArray':['foo','bar','baz',]})。此外,虽然我也有点新,但我认为驱散只使用var而不是$ scope。因为如果你只是创建一个变量然后稍后在你的函数中进行处理,你就不需要它在范围内。

Load data from file

Judging by your unused $http service, I think you may be asking about how to get the data from a .json file you have stored? For that, do something along the following:

根据你未使用的$ http服务判断,我想你可能会问如何从你存储的.json文件中获取数据?为此,请执行以下操作:

$http.get("video.json").then(function (response) {
    $scope.videos = response.data;
});

Hope this helps!

希望这可以帮助!