javascript - Angular.js : Error: Module 'MyApp' is not available

标签 javascript html angularjs module

我想使用this plunk在我的机器本地。但是,当我使用本地 Python 服务器或 http-server 运行它时,我不断收到以下错误:

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.

我的html 文件如下所示:

<!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>

ma​​in.js 文件如下所示:

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);
}]);

我不明白我做错了什么。我已经搜索过类似的错误消息,但没有找到任何可以帮助我解决问题的方法。

最佳答案

您在 angularjs 文件之前加载脚本文件,这就是您收到此错误的原因。 因此,在“angular.js”文件之后添加“main.js”文件。

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<script scr="main.js"></script>

关于javascript - Angular.js : Error: Module 'MyApp' is not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272275/

相关文章:

javascript - HTML5 模式与哈希模式 AngularJS 的优点/缺点

javascript - 如何获取 Firestore 查询中的第二个 10 文档?

c# - 将 Html.Partial 添加到 Javascript

javascript - 使用单选按钮更改变量值

html - 如何通过 HTML5 流式传输视频?

javascript - AngularJS 路由添加任何特殊字符吗?

javascript - 将过滤后的数据分配给 ng-if 中的 Controller 变量

javascript - 复制数组的元素并推送它会导致 "Duplicates in a repeater are not allowed."错误 - Angular

javascript - 使用 Javascript 更改完整的 DIV 内容

javascript - Haxe + Webpack 导出空对象