4

I am trying to convert csv to array on angular 1.5+ionic on hebrew. I am using ng-csv. What I done till now:

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>

my controller is :

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

what else should be done to get the object as array.

2 Answers 2

2

Modify the code in your controller as

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

and your in your 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 will now be an array of objects

Sign up to request clarification or add additional context in comments.

1 Comment

hi maybe you know how can I put string to this component to parse it as array? stackoverflow.com/questions/37700699/…
0

$scope.csv.result will contain the result array from conversion. After the user selects a CSV file from the browse button, it will be populated. So, if you just wanted to do a data dump to the screen:

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

Chances are good you want to do more than that when it gets populated, so you can set up a watch on it

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

2 Comments

lookd working but on first load app its getting error controllers.js:49Uncaught ReferenceError: csv is not defined(…)(anonymous function) @ VM2442:1InjectedScript._evaluateOn @ (program):145InjectedScript._evaluateAndWrap @ (program):137InjectedScript.evaluateOnCallFrame @
That was my error, the first function passed to $watch needs to return what's being watched, so change it to function() { return $scope.csv.content; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.