gpt4 book ai didi

javascript - 如何将来自 http json 的数据放入新的范围变量 [angularJS]

转载 作者:行者123 更新时间:2023-11-30 10:00:16 25 4
gpt4 key购买 nike

我编写了一个 $http.get 函数来提取存储在数据库中的数据。

我想在 AngularJS 的下拉函数中使用数据库数据。现在这意味着我必须创建一个新的 $scope JSON 数据变量并将实际的数据库数据存储在其中,以便我可以在我的 HTML 页面中访问它。

<select popover="Choose your input drug, type to filter list of drugs" data-ng-model="currentData"
data-ng-options="name for name in ourDataName" style="width:300px;" convert-to-number >
</select>
data-ng-options 中的

ourDataName 是我在 Controller 中定义为 $scope.ourDataName 的变量,它应该里面有所有的药物名称。我可以执行下面 get 函数中所写的操作;然而,问题是有超过 100 个药物名称:

$http.get(URL).success(function (data) {  //Note URL is where I stored my database. I did not put it for privacy reasons
$scope.ourDataName =
[
data[1].drug_name,
data[2].drug_name,
data[3].drug_name
]
});

我试着在 JSON 数据上放置一个 for 循环:

for(var i=0; i<100;i++)
{
$scope.ourDataName =
[
data[i].drug_name
]
}

问题是它最终只显示了第 100 个药物名称,因为它显示了循环结束的位置。

我没有解决方案。有什么方法可以遍历数据库并将它们存储在一个新变量中,而不必像我一开始那样遍历每个变量吗?

最佳答案

您不需要为下拉列表创建另一个数据集。

直接使用http get返回的数据。

这样试试

Controller

$http.get(URL).success(function (data) {        //Note URL is where I stored my database. I did not put it for privacy reasons
$scope.ourDataName=data;
});

查看

<select popover="Choose your input drug, type to filter list of drugs"  data-ng-model="currentData"
data-ng-options="name.drug_name for name in ourDataName" style="width:300px;" convert-to-number >
</select>

关于javascript - 如何将来自 http json 的数据放入新的范围变量 [angularJS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com