Jacob
Jacob

Reputation: 91

AngularJS inject element in [] issue

why we need to put injection element '$stateProvider', '$urlRouterProvider' in [ ], i found some tutorial videos that they just remove them and working fine, so i remove the same as well, but facing problems when deploying to web server with following error.

My question here is why we need to put the injection element in [], thanks.

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=app&p1=Error%3A %…es%2Fangular%3Fv%3DZGvZMulw2S5nigpn4yQz50dopAtRo-XYhjiW0UOpfnM1%3A25%3A449 (http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=app&p1=Error%3A %%E2%80%A6es%2Fangular%3Fv%3DZGvZMulw2S5nigpn4yQz50dopAtRo-XYhjiW0UOpfnM1%3A25%3A449) )

var app = angular.module("app", ['ui.router', 'ngTouch', 'ngMaterial', 'ngMessages', 'ngAnimate'])

app.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider,  $urlRouterProvider) {
   $urlRouterProvider.otherwise('/category');

  $stateProvider
        .state('category', {
            url: '/category',
            templateUrl: '../../app/CategoryViewer/Category.html',
       controller: 'CategoryController'
    });
}]);

Upvotes: 0

Views: 86

Answers (2)

Atul Sharma
Atul Sharma

Reputation: 10675

After minifying angular is not able to locate injected modules.. So, either define them or use ng-annotate .

I just love to go with Inline array method to avoid application breaking and easy code..

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

MyController.$inject = ['$scope', '$route', 'Item', 'items', 'utils', 'shade'];

angular.module('myApp')
    .controller('MyController', MyController);

function MyController($scope,$route,Item,items,utils,shade) {
    // do something
}

Upvotes: 1

Drag13
Drag13

Reputation: 5988

As I know, Injecting using [] syntax grants that after minification process, angular will be able to find our controllers, services and so on.

Upvotes: 0

Related Questions