How to redirect to another route?

19,708 views
Skip to first unread message

Freewind

unread,
May 23, 2012, 12:41:00 AM5/23/12
to ang...@googlegroups.com
For example, there is a button "Create" in my page, and it has ng-click attribute:

    <button ng-click="create()">Create</button>

And in the `create()` function, I want to do something, then redirect to another route `#/show`, which is a client site route defined in angularjs:

    function MyCtrl($scope) {
        $scope.create = function() {
             // do something
             // how to redirect to `#/show`, another view and controller will be used
        }
    }


Freewind

unread,
May 23, 2012, 12:59:04 AM5/23/12
to ang...@googlegroups.com
I know now: $location.path('/show');

Notice it should NOT be `path('#/show')`;

Peter Andreas Molgaard

unread,
May 23, 2012, 1:16:35 AM5/23/12
to ang...@googlegroups.com
Perhaps you can elaborate why you dislike "location.path" ?
I typically instrument my apps with a stateMachine wrapping the $location management, so from my controllers I use the stateMachine service, however that might also not be what you are looking for...


Freewind

unread,
May 23, 2012, 1:21:17 AM5/23/12
to ang...@googlegroups.com
StateMachine? I've never think of that.

But with $location.path, I found it boring to type long uris again and again, e.g.

     $location.path('/channels/'+$rootScope.channel+'/articles/edit');

Will stateMachine simplify it?

Michael Wills

unread,
May 23, 2012, 11:13:25 AM5/23/12
to ang...@googlegroups.com
It could be cause instead of updating the path directly in your controller, you just update which state you want to show and let the state machine manage updating the location path.

Peter, are you using something like this?


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/VJwTE6B2iYoJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Misko Hevery

unread,
May 23, 2012, 7:50:09 PM5/23/12
to ang...@googlegroups.com
maybe I am missing somethin, but what is wrong with: <a href="#/path">cilck me</a>

arush

unread,
Nov 21, 2012, 9:23:46 PM11/21/12
to ang...@googlegroups.com, mi...@hevery.com
I am implementing stately.js so my app automatically figures out what URL to serve depending on the data passed into the state machine. It's definitely a structured way to handle your routing and is actually how ember.js is built


Arush

Arnþór Snær Sævarsson

unread,
Mar 17, 2013, 6:16:15 AM3/17/13
to ang...@googlegroups.com, mi...@hevery.com
>maybe I am missing somethin, but what is wrong with: <a href="#/path">cilck me</a>

Freewind wants to perform some action and then redirect to a path, all from a single button click by a user.

Kowthal ganesh

unread,
May 15, 2014, 5:38:53 AM5/15/14
to ang...@googlegroups.com
Hi guys,
I want to share my source with you i have some problem on routing, am new for angular js

'use strict';

var sampleApp =    angular.module("myapp", [])
                   
sampleApp.controller("MyController", function($scope, $location, $window) {
                        $scope.doClick = function() {
                       //By using this location path i just embed the path in url it was reflecting there, but it was not routing to the page that i have mentioned below.
                      $location.path('/home');
                           }
            });   
           
sampleApp.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {
    $routeProvider.when('/home', {
        templateUrl: 'pane.html',
        controller : 'MnController'});
    $routeProvider
    .otherwise({redirectTo: '/home'});
    }]);

Please suggest me to recover from these bug

Mickey Vashchinsky

unread,
May 15, 2014, 6:58:04 AM5/15/14
to ang...@googlegroups.com
Or you can use ui-router with which you define states, so you have "showState" among others (and you can nest them too) which has a template + controller that you need.

var myApp = angular.module("MyApp", ["ui.router"])
    .config(function($urlRouterProvider, $stateProvider) {
        // ...
        $stateProvider.state("showState", {
            url: "/show",
            controller: "ShowCtrl",
            templateUrl: "/templates/show.tpl.html"
        });
        // ...
    });

Then you can:

function MyCtrl($scope, $state) {
    $scope.create = function() {
        // do something
        // ...
        // redirect to `#/show`, another view and controller will be used
        $state.go("showState");
    }
}

Hope that was helpful.
Reply all
Reply to author
Forward
0 new messages