如何正确地将模块加载到angularJS中

时间:2014-11-03 16:55:59

标签: javascript angularjs

我的angularjs应用程序有以下布局,但我无法弄清楚如何将新模块/第三方模块添加到我的代码中。我正在使用路线来加载内容。

我想加载ng-imgcrop来裁剪一些图片。这些例子似乎很简单但我必须有点傻。

这是我的布局如何运作。总体html是:(我已经删除了所有无用的东西)

<!DOCTYPE html>
<html lang="en" ng-app="phonecatApp" ng-controller="ApplicationController">
    <head>
        <script src="scripts/jquery.min.js"      type="text/javascript"></script>
        <script src="bower_components/angular/angular.js"></script>
        <script src="bower_components/angular-route/angular-route.js"></script>
        <script type='text/javascript' src="js/ng-img-crop.js"></script>
        <!-- AngularJS -->
        <script src="js/app.js"></script>
        <script src="js/controllers.js"></script>
        <!-- Controllers -->
        <script type="text/javascript" src="controllers/application.js"></script>
        <script type="text/javascript" src="controllers/index.js"></script>
        <script type="text/javascript" src="controllers/picture.js"></script>
    </head>
    <body>
            <div ng-view></div>
    </body>
</html>

页面由controllers.js

加载
angular.module('phonecatApp', ['ngRoute']).config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
             when('/picture/', {
                templateUrl: 'views/picture.html',
                controller: 'PictureController'
            }).
            otherwise({
                templateUrl: 'views/login.html',
                controller: 'LoginController'
            });
    }]);

我尝试复制我的picture.js图片控制器的示例:

 angular.module('phonecatApp', ['ngImgCrop']).controller('PictureController', function($scope, $http, $location) {
    $scope.thisController = 'PictureController';
    $scope.myImage='';
    $scope.myCroppedImage='';

    var handleFileSelect=function(evt) {
      var file=evt.currentTarget.files[0];
      var reader = new FileReader();
      reader.onload = function (evt) {
        $scope.$apply(function($scope){
          $scope.myImage=evt.target.result;
        });
      };
      reader.readAsDataURL(file);
    };
    angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
});

但是我收到以下错误:

 Error: [ng:areq] Argument 'ApplicationController' is not a function, got undefined
http://errors.angularjs.org/1.3.0/ng/areq?p0=ApplicationController&p1=not%20a%20function%2C%20got%20undefined
    at http://sssss/bower_components/angular/angular.js:80:12
    at assertArg (http://sssss/bower_components/angular/angular.js:1610:11)
    at assertArgFn (http://sssss/bower_components/angular/angular.js:1620:3)
    at http://sssss/bower_components/angular/angular.js:8319:9
    at http://sssss/bower_components/angular/angular.js:7496:34
    at forEach (http://sssss_components/angular/angular.js:343:20)
    at nodeLinkFn (http://sssss/bower_components/angular/angular.js:7483:11)
    at compositeLinkFn (http://sssss/bower_components/angular/angular.js:6991:13)
    at publicLinkFn (http://sssss/bower_components/angular/angular.js:6870:30)
    at http://sssss/bower_components/angular/angular.js:1489:27

如果我将以下行添加到ApplicationController而不是图片中,我没有错误,但我的路线都没有加载。

angular.module('phonecatApp', ['ngImgCrop']).controller('ApplicationController', function($scope, $location) {

});

1 个答案:

答案 0 :(得分:0)

因为你可以在app.js中编写多个控制器。根据此demo

修改您的代码
var myApp = angular.module('phonecatApp', ['ngRoute', 'ngImgCrop']).config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
    when('/picture/', {
      templateUrl: 'views/picture.html',
      controller: 'PictureController'
    }).
    otherwise({
      templateUrl: 'views/login.html',
      controller: 'LoginController'
    });
  }
]);



var pictureController = function($scope) {

  $scope.myImage = '';
  $scope.myCroppedImage = '';

  var handleFileSelect = function(evt) {
    var file = evt.currentTarget.files[0];
    var reader = new FileReader();
    reader.onload = function(evt) {
      $scope.$apply(function($scope) {
        $scope.myImage = evt.target.result;
      });
    };
    reader.readAsDataURL(file);
  };
  angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect);
}

myApp.controller('pictureController ', pictureController );