How to use angularjs config block?

753 Views Asked by At

I am new to angularJS and trying to learn it. I came across config function where I see two different arguments as in below example.

Example 1

dashboardApp.config(function($stateProvider, $urlRouterProvider) {
//$urlRouterProvider.when('', '/add');
$stateProvider.state('add', 
                 { 
                     url:'/add?month&year', 
                     templateUrl: 'partial/add.html', 
                     controller: 'AddListController' 
                 })
});

Example 2

gm.config(['$routeProvider', 'Path', function($routeProvider, Path) {
$routeProvider.when('/login', { 
    templateUrl: Path.view('application/authentication/login.html'), 
    controller: 'authController' 
});
}]);

In the first example, config has a function parameter. And in second example config has array parameter.

Can someone please explain to me what is the difference between these two approaches and when to use which?

3

There are 3 best solutions below

0
On

There are two differences here. The first example is providing configuration for Angular UI Router, whereas the second is using ngRoute.

The difference in syntax that you noted is the difference between having code that will not withstand minification (first example) and code that will withstand minification.

0
On

+1 to the previous answer pointing out $stateProvider is angular ui-router syntax. I would also point out that declaring $routeProvider has different syntax than other angular items like services and controllers.

0
On

You should use ui-route provider as it is better than ngRoute, ui-router allows you to code your templates in a nested form