作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个 $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/
我是一名优秀的程序员,十分优秀!