之前,我的查询在示例 1 页(index.html)项目中工作,但这次,我正在处理一个具有选项卡的项目(我通过 Ionic Lap 创建了带有选项卡的项目)。我需要在 tab-home.html 页面中打印 JSON 文件的输出。 JS 和index.html 不在同一个本地。
标签页位于/Template 文件夹中(tabs.html、tab-home.html、tab-account.html) JS位于/JS文件夹中(app.js、controllers.js、services.js和employees.json)
这是我的代码
在controllers.js中
.controller('HomeCtrl', function($scope, webtest) {
webtest.fetch().then(function(data) {
$scope.data = data;
})
})
在 services.js 中
.factory('webtest', function($q, $timeout, $http) {
var Webtest = {
fetch: function(callback) {
return $timeout(function() {
return $http.get('employees.json').then(function(response) {
return response.data;
});
}, 30);
}
};
return Webtest;
});
在 tab-home.html
<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="item in data">
<h2>{{item.product_name}}</h2>
</ion-item>
我没有碰app.js和index.html。他们有默认代码。
谢谢大家, 穆罕默德。
最佳答案
您的回复应类似于 {"data": {....
如果没有:
return $http.get('employees.json').then(function(response) {
return response;
});
关于javascript - Angular JS - 无法读取本地 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36597808/