2016-08-08 31 views
0

我有這個ReadCSV.js我在哪裏,從角JS通csv文件內容到節點JS和JS節點解析在蒙戈DB插入

app.get('/index.html', function(req, res) { 
    res.sendFile(__dirname + '/index.html'); 
}); 

app.post('/add_event', function(req, res, next) { 
    console.log("Node JS File1 :" +req); 
    var finalData = req.body.test;//value is undefined 
    console.log("Node JS File : " + finalData);//value is undefined 

的index.html

檢索csv文件內容
var myapp = angular.module('myapp', []); 

myapp.controller('MainCtrl', function ($scope,$http) { 
$scope.showContent = function($fileContent){ 
    $scope.content = $fileContent; 
var test=  $fileContent; 
alert(test); 
}; 

$scope.submit = function(test){ 
// 
$http.post('/add_event', $fileContent) 
     .success(function(){ 
      console.log("here" +$fileContent); 
    }); 
} 


<body ng-controller="MainCtrl"> 
<form ng-submit="submit()"> 
<div class="test"> 
<div class="panel-heading"> 
    <h3 class="panel-header">Upload Events</h3> 
</div> 
    <input class="btn btn-primary" type="file" on-read-  
    file="showContent($fileContent)"/></br> 
    <button class="btn btn-primary btn-block" ng-click="processData(content)" 
    type="submit"> 
    Upload File 
    </button> 
    </div> 
</form> 
    <div class="test2"> 
    {{content}} 
    </div> 

我的價值是未定義的。 如何將csv內容從角度js傳遞到節點js並解析它,以便我可以在mongodb中插入。

回答

0

請嘗試下面的代碼,注意我們使用this> $ scope,Angular文檔建議這樣做。

我一直在使用json文件,所以我不一定知道這是否會與csv文件一起工作。

MyApp.query(); 
    angular. 
     module('myApp'). 
     controller: ['MyApp', 
     function MyAppController(MyApp) { 
      this.myApp = MyApp.query(); 
      } 
     ] 
     }); 

您檢索來自CSV/JSON文件中的數據後,你應該使用一個ngRepeat在裏顯示的數據要在HTML主體部分

<li ng-repeat="data in $ctrl.myApp" 
    <p>{{myApp.data}}</p> 
    </li> 

我希望這顯示以某種方式幫助它,祝你好運。