2016-05-29 73 views
3

我想将csv转换为角度1.5 +希伯来语离子数组。 我正在使用ng-csv。 我做了什么至今:如何将csv转换为数组

HTML:

<ng-csv-import 
    class="import" 
    content="csv.content" 
    header="csv.header" 
    header-visible="csv.headerVisible" 
    separator="csv.separator" 
    separator-visible="csv.separatorVisible" 
    result="csv.result" 
    encoding="csv.encoding" 
    encoding-visible="csv.encodingVisible"></ng-csv-import> 

我的控制器:

.controller('SettingsCtrl', function ($scope,$parse) { 
    $scope.csv = { 
     content: null, 
     header: true, 
     headerVisible: true, 
     separator: ',', 
     separatorVisible: true, 
     result: null, 
     encoding: 'Windows-1255', 
     encodingVisible: true 
    }; 

还有什么应该怎样做才能获取对象的数组。

回答

1

在控制器修改代码,

 $scope.csv = { 
     content: null, 
     header: true, 
     headerVisible: true, 
     separator: ',', 
     separatorVisible: true, 
     result: null, 
     encoding: 'ISO-8859-1', 
     encodingVisible: true, 
     accept:".csv" 
    }; 

和你在你的HTML

 <ng-csv-import content="csv.content" 
      header="csv.header" 
      separator="csv.separator" 
      result="csv.result" 
      accept="csv.accept"> 
     </ng-csv-import> 

$ scope.csv.result现在将对象的数组

+0

嗨,也许你知道我怎么能把这个组件的字符串解析为数组? http://stackoverflow.com/questions/37700699/parse-string-csv-to-array-on-angular –

-1

$scope.csv.result将包含转换的结果数组。用户从浏览按钮中选择一个CSV文件后,它将被填充。所以,如果你只是想做一个数据转储到屏幕:

<div ng-if="csv.content"> 
    {{csv.content}} 
</div> 

机会是很好的,你想要做的比这更多的是被填充的时候,这样你就可以建立一个观察它

// (in controller) 
$scope.$watch(
    function() { return $scope.csv.content; }, 
    function(newValue, oldValue) { 
     // do something on change 
    } 
); 
+0

lookd工作,但第一次加载应用其收到错误controllers.js:49Uncaught的ReferenceError:CSV是没有定义的(...)(匿名函数)@ VM2442: 1InjectedScript._evaluateOn @(program):145InjectedScript._evaluateAndWrap @(program):137InjectedScript.evaluateOnCallFrame @ –

+0

这是我的错误,传递给$ watch的第一个函数需要返回正在监视的内容,所以将其更改为function(){return $ scope.csv.content; }' – jdphenix