繁体   English   中英

无法从后端到前端的角度选择选项获取文件列表?

[英]Unable to fetch list of files from back-end to front-end angular select options?

我正在使用node.jsserver.js文件中的目录('.public / jsonfiles')获取文件。 但是我无法使用AngularJSNode.js服务将这些文件列表显示在视图页面上。

错误:

我正在获取数据data.forEach不是函数错误(在返回语句上,我可以在控制台中看到总的html内容,而不是服务文件中的文件列表: return $http.get('/public/jsonfiles');

否则,如果我给出: return $http.get('').then(function(data){ console.log(data)}); / giving Main.get()成功不是一个函数,并且无法html content on console提供全部html content on console

创建存储库

我看了你的回购。 根据要求

 return $http.get('/public/jsonfiles');

节点将返回index.html,这将对data.forEach造成MainCtrl错误,因为data不是数组。

如果您安装serve-index并使用它为公众提供目录列表,它应该可以工作。

var serveIndex = require('serve-index');
app.use('/public',serveIndex('public'));

(server.js)

要加载文件json,请向MainService添加另一个方法以获取文件。

    getFile: function (filename) {
        //no public in the path because node is removing when it serves static files
        return $http.get('/jsonfiles/' + filename);
    }

在MainCtrl中,可以在更改选定文件时加载文件内容。

  $scope.optionChanged = function () {
     Main.getFile( $scope.selectedjsoncontent )
         .success(function(result){
           $scope.textAreaData = result;
         });    

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM