Angularjs没有加载json文件
Angularjs not loading json files
我不知道为什么当我在一个html页面上打印json文件的工作,也从一个按钮调用一个函数,但不是在javascript文件。
这是一个问题,因为我需要排序json文件中的数据之前显示它从网页,我不能使它工作。我尝试使用这个解决方案https://stackoverflow.com/a/15463124/2796268,但是控制台说
我代码:jsonDat未定义
$scope.init = function () {
console.log("init");
$http.get('json/file.json') .success(function(data) {
$scope.jsonDat = res.data;
})
.error(function(data,status,error,config){
$scope.jsonDat = [{heading:"Error",description:"Could not load json data"}];
});
console.log(jsonDat);
};
如何处理json数据之前的页面加载或当页面加载?
您可以在从$http
返回数据时处理数据,如下所示:
$scope.init = function () {
$http.get('json/file.json').success(function(data) {
//---- SORT HERE ----
$scope.jsonDat = mySortFunction(data);
});
};
试试这个:
$scope.init = function() {
console.log("init");
$http.get('json/file.json').success(function(data) {
$scope.jsonDat = data;
console.log($scope.jsonDat);
})
.error(function(data, status, error, config) {
$scope.jsonDat = [{
heading: "Error",
description: "Could not load json data"
}];
console.log($scope.jsonDat);
});
};
在成功中,你有数据,但你试图从res.data中获取。当你使用success时,它不是对数据的响应,而是对数据的响应。
我以为你想排序一些JSON文件,然后显示在HTML页面。所以我的想法是得到JSON文件(你试过)
$http.get('json/file.json') .success(function(data) {
$scope.jsonDat = data.res;
console.log('Checking the result',angular.toJson($scope.jsonDat));
})
但是把结果放在
$scope.jsonDat = data.res;
,
sortService.sortJsn = data.res;
美元范围。jsonDat而不是创建angular service把你的数据倒在那里,然后你可以在你的控制器排序的任何地方访问这些数据,它也显示在HTML中。
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 不显示带有本地json文件数据的谷歌地图脚本
- 可以't使用Appcelerator将JSON文件解析为JavaScript中的TableView
- 加载两个具有相同父密钥名称的json文件
- 使用JSON文件中的变量(字符串)填充文本区域
- 动态显示JSON文件内容
- 正在尝试将JSON文件放入JS数组
- 如何检查Json文件更新,如果更新了,则用更新的数据刷新我的页面
- 单击按钮更改加载到表中的JSON文件
- 处理一个JSON文件;完全相同的副本不是
- 在Javascript中加载JSON文件
- 使用Bootstrap将JSON文件加载到表中
- Angular 2:在本地.json文件上找不到文件
- JSON-从浏览器向外部服务器发送哪些http头JSON文件
- 如何获取d3.js中json文件中具有特定值的总行数
- 如何在dojo应用程序构建概要文件中加载json文件
- jQuery UI使用json文件自动完成
- 通过Azure存储以HTML形式获取JSON文件
- ngResource没有'从JSON文件解析HTML时不起作用