简体   繁体   中英

How to load modules in to angularJS correctnly

I have the following layout to my angularjs app but I can't work out how to add new modules/3rd party modules to my code. I'm using routes to do the loading of content in.

I want to load in ng-imgcrop to crop some images. The examples seem simple enough but I must be being a bit silly.

Heres how my layout works. The overarching html is: (i've removed all the useless stuff)

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

The pages get loaded in by 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'
            });
    }]);

I tried copying the example for that for my picture.js picture controller:

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

However I get the following errors:

 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

If I add the following line to the ApplicationController instead of picture I get no errors but none of my routes load.

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

});

Since you can write multiple controllers inside app.js. Modify your code based on this 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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM