Last Updated: February 25, 2016
·
724
· cboden

ng Functional only controller

Recently I was writing an AngularJS logout controller. This controller was to only do one thing: change the state and redirect the user to /. The trouble was it was not being executed. As it turned out Angular requires some kind a template or templateURL for all routes. The solution was to set a template of a space:

angular.module('myapp.logout', [])

.config(function($routeProvider) {
    $routeProvider
    .when('/logout', {
        template: ' ', // <-- this is the important part
        controller: 'LogoutCtrl'
    }
})

.controller('LogoutCtrl', function($location) {
    // do logout stuff

    $location.path('/');
})

1 Response
Add your response

You could also check out https://github.com/angular-ui/ui-router.
It supports multiple views in one state and uses states instead of routes so your goal of not needing a template could be accomplished :)

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#angularjs
Did you know?

AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.

Security insights from Cside