Search code examples
angularjstodomvc

how does todo mvc example for angularjs do without ng-controller?


Every example I've looked at has made use of the ng-controller directive to get things working.

The Todo MVC example at https://github.com/tastejs/todomvc/tree/gh-pages/examples/angularjs creates a 'TodoCtrl' controller. But in the corresponding index.html, there is no use of the ng-controller directive.

How is this possible? and why did they choose to do it this way?


Solution

  • It uses the ngRoute provider.

    angular.module('todomvc', ['ngRoute'])
        .config(function ($routeProvider) {
            'use strict';
    
            var routeConfig = {
                controller: 'TodoCtrl',//add controller to view
                templateUrl: 'todomvc-index.html',
                resolve: {
                    store: function (todoStorage) {
                        // Get the correct module (API or localStorage).
                        return todoStorage.then(function (module) {
                            module.get(); // Fetch the todo records in the background.
                            return module;
                        });
                    }
                }
            };
    
            $routeProvider
                .when('/', routeConfig)
                .when('/:status', routeConfig)
                .otherwise({
                    redirectTo: '/'
                });
        });