javascript - Angular JS - 无法读取本地 JSON 文件

标签 javascript angularjs json ionic-framework

之前,我的查询在示例 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/

相关文章:

javascript - 将信用卡刷卡者的月份读取为全名而不是数字

javascript - jquery $ajax 没有按预期工作

javascript - 按类别对 JSON 内容进行排序

jquery - 是否可以在 Chart.js 的雷达图中设置扇区的背景颜色?

JQuery $.each 仅返回 JSON 的第一项

JavaScript:我传入一个列表,尝试访问索引,它说变量未定义

javascript - 使用 AngularJS 单击搜索结果时将输入重置为初始值

javascript - 通过 Angular JS 中的 ajax 调用加载模板 url

ios - 客户端服务器 json 响应

php - JS - 使用路径时数组 php 出现 PHP JSON 错误