2

I would like to use this plunk locally on my machine. However, when I either run it with the local Python server or http-server, I keep getting the following Error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due     to:
    Error: [$injector:nomod] Module 'myApp' is not available! You either     misspelled the module name or forgot to load it. If registering a module ensure      that you specify the dependencies as the second argument.

My html file looks like this:

<!DOCTYPE html>
<html ng-app="myApp">

  <head lang="en">
    <meta charset="utf-8" />
    <title>Custom Plunker</title>
    <script scr="main.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.1/papaparse.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-file-upload/1.1.5/angular-file-upload.min.js"></script>
    <link rel="stylesheet" href="main.css">
  </head>

  <body ng-controller="MyCtrl">
    <h1>CSV</h1>
    <div>
      <input type="checkbox" ng-model="append">
      Append to existing on drag & drop
    </div>
    <div class="drop-container" nv-file-drop nv-file-over uploader="uploader">
      <textarea ng-model="csv" placeholder="Enter your CSV here, or drag/drop a CSV file"></textarea>
    </div>

    <h1>D3 Flare JSON</h1>
    <div>
      <input type="checkbox" ng-model="compact"> Compact
    </div>
    <div>
      <input type="text" ng-model="tags.parent" placeholder="parent tag">
      <input type="text" ng-model="tags.children" placeholder="children tag">
      <input type="text" ng-model="tags.leaf" placeholder="leaf tag">
      <input type="text" ng-model="tags.size" placeholder="size tag">
    </div>
    <textarea readonly ng-model="json"></textarea>
  </body>

</html>

And the main.js file looks like this:

CSV to D3 Flare JSON converter in AngularJSPreview Edit Code
index.html
main.js
main.css
main.js
angular.module('myApp', ['angularFileUpload'])

.factory('FlareJson', ['$q', function($q) {
  function updateTree(curr, arr, tags) {
    if ((arr.length || 0) < 2) {
      return;
    }

    if (!curr.hasOwnProperty(tags.children)) {
      curr[tags.children] = [];
    }

    var elem;    
    if (arr.length == 2) {
      elem = {};
      elem[tags.leaf] = arr[0];
      elem[tags.size] = arr[1];
      curr[tags.children].push(elem);
    } else {
      curr[tags.children].some(function(e) {
        if (e[tags.parent] == arr[0] || e[tags.leaf] == arr[0]) {
          elem = e;
          return true;
        }
      });
      if (!elem) {
        elem = {};
        elem[tags.parent] = arr[0];
        curr[tags.children].push(elem);
      }
      updateTree(elem, arr.slice(1), tags);
    }
  }

  function buildJson(csv, compact, tags) {
    var deferred = $q.defer();

    var result = {};
    result[tags.parent] = 'flare';

    Papa.parse(csv, {
      header: false,
      dynamicTyping: true,
      complete: function(csvArray) {
        csvArray.data.forEach(function(line) {
          if (line.length) {
            updateTree(result, line, tags);
          }
        });
        if (compact) {
          deferred.resolve(JSON.stringify(result));
        } else {
          deferred.resolve(JSON.stringify(result, null, 2));
        }
      }
    });

    return deferred.promise;
  }

  return buildJson;
}])

.controller('MyCtrl', ['$scope', 'FileUploader', 'FlareJson',
function($scope, FileUploader, FlareJson) {
  $scope.csv = "";
  $scope.compact = false;
  $scope.json = "";
  $scope.tags = {
    parent: 'skill',
    children: 'children',
    leaf: 'name',
    size: 'level'
  };

  $scope.uploader = new FileUploader();

  $scope.uploader.onAfterAddingFile = function(fileItem) {
    var reader = new FileReader();
    reader.onloadend = function(event) {
      $scope.$apply(function() {
        if ($scope.append) {
          $scope.csv += event.target.result;
        } else {
          $scope.csv = event.target.result;
        }
      });
    };
    reader.readAsText(fileItem._file);
  };

  function update() {
    FlareJson($scope.csv, $scope.compact, $scope.tags).then(function(json) {
      $scope.json = json;
    });
  }

  $scope.$watchGroup(['csv', 'compact'], update);
  $scope.$watchCollection('tags', update);
}]);

I don't understand what I'm doing wrong. I already searched for similar error messages, but nothing that I found could help me to solve my problem.

3 Answers 3

2

You load your script file before angularjs file that's why you are getting this error. So, Add your "main.js" file after "angular.js" file.

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<script scr="main.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help, I put the main.js script at the end of my script definitions, but I'm still getting the same error Error: [$injector:modulerr] Failed to instantiate module myApp due to: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
Roy.23, this was a problem as well and you were the first to answer, so, I'm accepting. Thank you!
0

I believe it's because you're loading your main.js before you load Angular. Try putting your script at the end of the script definitions:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.1/papaparse.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-file-upload/1.1.5/angular-file-upload.min.js"></script>
<script scr="main.js"></script>

1 Comment

Hi Alex, I tried, but I'm still getting the same error... (?)
0

Oh, solved! Turns out, that the following couple of first lines in main.js were causing the trouble:

CSV to D3 Flare JSON converter in AngularJSPreview Edit Code
index.html
main.js
main.css
main.js

I removed them from main.js, now it works - yuhuu! :)

Comments

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.