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.

1 Answer 1

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')
    }]);
});
Sign up to request clarification or add additional context in comments.

3 Comments

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?
You can see this SO post for possible redirection issues. Set breakpoints in your router, see if your URL updates, look for console errors, etc.
might be useful to anyone coming along to point out what you had to do to fix your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.