2016-05-29 70 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