Harshal Yelpale
Harshal Yelpale

Reputation: 535

How to redirect a page based on condition in angularjs

I want to redirect a page based on condition after logged in by user at that time "varView" flag will have some values using cookies or rootScope or any other option. After that, in config while doing routing i have to check the flag and based on flag's value page should get redirect.

Here's code:

$routeProvider.when("/editTest/:testID",
{
  templateUrl: '/apps/templates/xyz/editTest.html',
               controller: 'EditTestController',
               resolve: {
    load: function (ShareData, $location) {
    var varView = 'tabPanelView'; // this value of varView will get loaded from login
    if (varView == 'tabPanelView') {
      $location.path('/editTestPageView.html');
    } else {
      $location.path('/editTestTabPanelView.html');
    }
    return ShareData.get('cEditTest');
  }
}
});

If varView == 'tabPanelView' then redirect on editTestPageView.html page else redirect it to editTestTabPanelView.html.

Actually, i have a customer form with two diff. views (2 html files) i.e. one with normal page scrollable with number of sections and other one is in Tab Panel view mode which is sections divided in tabs without scrollable.

So, the page view settings is on user's hand in admin area. User will choose which form view he wanted and according to that the page should get open.

Upvotes: 1

Views: 1742

Answers (3)

Harshal Yelpale
Harshal Yelpale

Reputation: 535

Finally, ended with pass the parameters in url like

'<a href="#/editTest/{{model._id}}/' + getCookie(escape('cformview'))+ '" title="Edit Test"></a>'

And in $routeProvider

   $routeProvider.when("/editTest/:ID/:flagTab",
                        {                               
                             templateUrl: function (params) {
                                var flag = params.flagTab;                                    
                                if (flag == 'tabPanelView') {
                                    return '/apps/templates/editTest.html';
                                }
                                else {
                                    return '/apps/templates/editTestPageView.html';
                                }                                    
                            },
                            controller: 'EditTestController'
                        });

Upvotes: 1

Aarati
Aarati

Reputation: 321

May be try to pass the "userpreference" as part of path For example:/editTest/:testID/:varView and inside routeProvider.

$routeProvider    
.when("/red/:testID/:varView", {        
    templateUrl: function(params) {         
    return params.varView == 'table'? "./pages/table.htm" : "./pages/scroll.htm";
  },controller  : 'aboutController'        
}) 

OR, can have template with in template inside HTML file using either,

    <div ng-switch-when="1">  OR <ng-include src="subPage"></ng-include>

You may take a look at below answer:

using $rootScope in templateUrl function

Upvotes: 1

Maccurt
Maccurt

Reputation: 13817

You should be able to simply use the $window service. With an if condition you can use $window.open to go wherever you like. If that did not answer your question comment on why and I will try again. You will have to put the route in the open method.

Upvotes: 0

Related Questions