我使用node.js
从server.js
文件中的目录('.public/jsonfiles')获取文件。但我无法使用 AngularJS
和我的 Node.js
服务在我的 View 页面上显示这些文件列表。
错误:
我收到 data.forEach
is not a function 错误(我可以在控制台中看到总的 html 内容,而不是在 return 语句中看到服务文件中的文件列表:return $ http.get('/public/jsonfiles');
否则,如果我给出: return $http.get('').then(function(data){ console.log(data)});
/giving Main.get( )
success 不是一个函数,并且在控制台上给出 html 内容的总数
已创建 repository .
最佳答案
我看了你的仓库。根据要求
return $http.get('/public/jsonfiles');
Node 将返回index.html,这将在data.forEach上导致MainCtrl错误,因为数据不是数组。
如果您安装serve-index并使用它为 public 提供目录列表,它应该可以工作。
var serveIndex = require('serve-index');
app.use('/public',serveIndex('public'));
(服务器.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;
});
}
关于javascript - 无法从后端到前端 Angular 选择选项获取文件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39867679/