角度控制器错误:$ injector:modulerr模块错误

时间:2016-09-15 14:22:23

标签: angularjs gruntjs uglifyjs

我正在尝试修改我的AngularJS代码。我按照互联网上的所有教程,现在有了以下代码,但是我仍然得到了以及uglifying后的错误:

var app = angular.module('myapp', ['ngMaterial', 'pascalprecht.translate', 'ngSanitize', 'material.components.expansionPanels', 'ngFileSaver', 'ngMessages', 'ngFileUpload', 'ngStorage']

TestController.$inject = ['$scope', '$translate', '$timeout','$mdSidenav', '$log', '$sessionStorage'];

angular.module('myapp').controller('TestController', TestController);

function TestController ($scope, $translate, $timeout, $mdSidenav, $log, $sessionStorage) {}

编辑: 我发现了这个问题。问题是我将模块配置为:

app.config(function ($locationProvider) {
    $locationProvider.html5Mode(true);
});

而不是:

app.config(['$locationProvider', function ($locationProvider) {
    $locationProvider.html5Mode(true);
}]);

app.directive('onReadFile', function ($parse) {
    return {
        restrict: 'A',
        scope: false,
        link: function(scope, element, attrs) {
            var fn = $parse(attrs.onReadFile);

            element.on('change', function(onChangeEvent) {
                var reader = new FileReader();

                reader.onload = function(onLoadEvent) {
                    scope.$apply(function() {
                        fn(scope, {$fileContent:onLoadEvent.target.result});
                    });
                };

                reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
            });
        }
    };
});

2 个答案:

答案 0 :(得分:-1)

this question可能重复。

尝试以这种方式使用它:

angular.module('myapp').controller('TestController', ['$scope', '$translate', '$timeout','$mdSidenav', '$log', '$sessionStorage', function checkInCtrl ($scope, $translate, $timeout, $mdSidenav, $log, $sessionStorage){}]);

答案 1 :(得分:-1)

在尝试验证代码之前,请务必尝试确保您的应用正常运行。

与使用控制器时相同,在设置指令时使用相同的方法,使用$ inject来声明依赖项。

见下文

app.directive('onReadFile',onReadFile);

onReadFile.$inject = ['$parse'];

function onReadFile($parse){

  var directive = {
    restrict:'A',
    scope: false,
    link: link
  }

  return directive;

  //////////

  function link(scope, element, attrs){

      var fn = $parse(attrs.onReadFile); 

      element.on('change', function(onChangeEvent) { 

        var reader = new FileReader(); 

        reader.onload = function(onLoadEvent) { 
          scope.$apply(function() { 
            fn(scope, {$fileContent:onLoadEvent.target.result}); 
          }); 
        }; 

        reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]); 
      }); 

  }// end link

}// onReadFile