如何使用AngularJS从Json中检索数据
How to retrieve data from Json using AngularJS?
我最近尝试使用AngularJS从JSON文件中检索数据。但是我不知道该怎么做;我曾试图这样做,但它不工作,有人能告诉我我错在哪里?
<div ng-repeat="stuff in otherStuff">
<p>Title: {{stuff.name}}</p>
<p>Url: {{stuff.url}}</p>
</div>
DataController:
$scope.otherStuff = {};
jsonFactory.getOtherStuff()
.then(function (response) {
$scope.otherStuff = response.data.components;
}, function (error) {
console.error(error);
});
jsonFactory:
angular.module('generatorMeanstackApp')
.factory('jsonFactory', function ($q, $http) {
return {
getOtherStuff: function () {
var deferred = $q.defer(),
httpPromise = $http.get('data.json');
httpPromise.then(function (response) {
deferred.resolve(response);
}, function (error) {
console.error(error);
});
return deferred.promise;
}
};
});
JSON数据示例:
{
"name": "Blogs",
"url": "blogs.my-media-website.com/*",
"dependsOn": ["Wordpress MU"],
"technos": ["PHP", "Wordpress"],
"host": { "Amazon": ["?"] }
},
在你的控制器中,对数据的引用(通过service)嵌套得太深,它应该是:
$scope.otherStuff = response.data;
代替:
$scope.otherStuff = response.data.components;
你可以把JSON包在一个数组中:
[
{...},
{...},
{...}
]
此外,如果您与约曼支架,data.json
需要在app/
路线上。
相关文章:
- 如何在corona sdk中从CK编辑器中检索数据
- XML2JSON并检索数据
- 如何基于两个下拉选择从数据库中检索数据
- 在检索数据时是否可以停止图像加载请求
- 正在从JSON对象检索数据
- 只有当Angular从资源对象检索数据时,才选择选项元素
- 如何使用JavaScript从文本框中检索数据
- html表单,它有文本和表,现在我想在一个简单的文本文件中保存和检索数据,如何实现它
- 我想使用纯Javascript从MS Dynamics CRM Online 2015添加/检索数据
- Firebase-正在从推送项目中检索数据
- 为什么可以't我从Json文件中检索数据
- 从 tinymce 文本区域检索数据时出现问题
- 从 ajax 函数从数据库中检索数据值
- 检索数据表中的指定行
- 调用c#函数从传递javascript变量的javascript中检索数据
- 使用jqueryAjax和php脚本从mysql检索数据时遇到的问题
- JQuery:从URL检索数据
- SharePoint 2010 Rest API错误(未检索数据)
- JQuery检索数据,但如果退出,则获取默认值
- jQuery,用于检索数据的变量