1

I'm experimenting with angular.js and require.js. What I'm trying to do is a simple login form module. My project is based on the https://github.com/partap/angular-requirejs-seed project.

So, I have the routes:

angular.module('app', [])
    .config([ '$routeProvider',
        function ($routeProvider) {
            $routeProvider.when('/auth', {
                templateUrl : 'forms/auth.html',
                controller : ...
            });
            $routeProvider.when('/account', {
                templateUrl : 'forms/account.html',
                controller : ...
            });
            $routeProvider.otherwise({ redirectTo : '/auth' });
        }]);

So when the application starts it navigates to the #/auth. It is ok. The auth controller is created as follows:

define([ 'angular' ], function (angular) {
    return function ($scope) {
        ... do something here ...
        ... and redirect to /account if credentials are valid ...
    };
});

Everything goes well until the redirection - I think that I should use the $location variable somehow, but do not know how to get it.

Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85

1 Answers1

0

You need to pass it in as a dependency to the module

define([ 'angular' ], function (angular) {
    return app.controller('yourController', ['$scope','$location', function ($scope, $location) {
        ... do something here ...
        $location.path('/account')
    }]);
});
Scott Silvi
  • 3,059
  • 5
  • 40
  • 63
  • Thanks! It works with the only correction that app.controller returns the application itself. But unfortunately the $location.path('/account') does nothing... It is another issue but maybe I just missed something trivial? – Alex Netkachov May 08 '13 at 21:03
  • 1
    You can see [this SO post](http://stackoverflow.com/questions/11907961/redirect-using-angularjs) for possible redirection issues. Set breakpoints in your router, see if your URL updates, look for console errors, etc. – Scott Silvi May 08 '13 at 21:41
  • might be useful to anyone coming along to point out what you had to do to fix your issue. – Scott Silvi May 09 '13 at 02:23