简体   繁体   中英

Angular: Error after concat angular js files into single js file with gulp

Im using gulp-concat to merge all angular js files into one but after running gulp task i get this error in chrome console on runing application :

angular.js:13708Error: [ng:areq] http://errors.angularjs.org/1.5.7/ng/areq?p0=userboxController&p1=not%20a%20function%2C%20got%20undefined

my gulp task :

gulp.task('scripts', function () {
  return gulp.src(['js/app/*.js', 'components/*/*/*.js'])  
    .pipe(concat('appscript.js'))
    .pipe(minify()) 
    .pipe(gulp.dest('./dist/js/'));
});

and gulp-concat merges dedicated angular js files into appscript.js like:

angular.module('app',[]);     


angular
    .module("app", [])
    .controller("paymentCtrl", ['$scope', '$http', function ($scope, $http) {
        $http.get('data/payments.json').then(function (payments) {
            $scope.payments = payments.data;
        });

        $scope.saveEntity = function () {
            console.info("goog");
        }
    }]); 

angular
    .module("app",[])
    .controller("userboxController", ['$scope', '$http',function ($scope, $http, usersService) {
        usersService.getCurrentUser().then(function (user) {
            $scope.user = user.data;
        });
    }]);


angular
    .module("app",[])
    .controller("usersController",['$scope', '$http','usersService', function ($scope, $http, usersService) {
        usersService.getAll().then(function (users) {
            $scope.users = users.data;
        });
    }]); 


angular
    .module("app", [])
    .directive('usersGrid', function () {
        return {
             templateUrl : 'components/users/template/grid.html'
        }
    }); 

what's wrong with angular?!!

This is not a merge related problem.

You are making app modules every time with

angular.module('app', [])

You have to initialise module only one place and you will be using same module every time with ~ [] brackets.

Please find the plunker here

var myApp = angular.module('app');

myApp.controller("paymentCtrl", ['$scope', '$http', function($scope, $http) {
  $http.get('data/payments.json').then(function(payments) {
    $scope.payments = payments.data;
  });

  $scope.saveEntity = function() {
    console.info("goog");
  }
}]);

myApp.controller("userboxController", ['$scope', '$http',       function($scope, $http, usersService) {
  $scope.user = 'abc';

}]);


myApp.controller("usersController", ['$scope', '$http', 'usersService', function($scope, $http, usersService) {
  usersService.getAll().then(function(users) {
    $scope.users = users.data;
  });
}]);


myApp.directive('usersGrid', function() {
  return {
    templateUrl: 'components/users/template/grid.html'
  }
});

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