[英]Unable to fetch list of files from back-end to front-end angular select options?
我正在使用node.js
从server.js
文件中的目录('.public / jsonfiles')获取文件。 但是我无法使用AngularJS
和Node.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.