javascript - How to redirect to home view after page refresh - AngularJS - Stack Overflow

Question Background:I have encountered what seems to be a mon issue with AngularJS where I am implement

Question Background:

I have encountered what seems to be a mon issue with AngularJS where I am implementing:

 $locationProvider.html5Mode({ enabled: true, requireBase: false });

This has removed the '#' from my URL routes and in-turn caused a 404 error. I fixed that by adjusting web.config and the page reloads now.

The Issue:

I have a 2 views called Home.html and Results.html. I also have a index.html which houses the in my app which houses a <div ui-view> where the home and result views are injected into.

The following is the routes used:

http://localhost:5XXXX/ - home
http://localhost:5XXXX/results - results

I want to redirect to the home.html view when results.html is refreshed. Currently when results.html is refreshed all of y scope data is lost along with jquery plugin object state in the directives.

The Code:

app.js - this houses the routes of the app:

 angular
  .module('app', [
    'ui.router',
    'ui.bootstrap',
    'ngAnimate',
    'ngResource',
    'rzModule',
    'angular-ladda',
    'jcs-autoValidate'
  ])
  .config(['$urlRouterProvider', '$stateProvider', '$locationProvider', function ($urlRouterProvider, $stateProvider, $locationProvider) {

      $urlRouterProvider.otherwise('/');
      $stateProvider
        .state('home', {
            url: '/',
            templateUrl: 'home.html',
            controller: 'HomeController'
        })


          $stateProvider.state('results', {
              url: '/results',
              templateUrl: 'Results.html',
              controller: 'ResultsController'
          })

          $locationProvider.html5Mode({
              enabled: true,
              requireBase: false
          });
    }]);

ResultsController.js:

var app = angular.module('app');

app.controller('ResultsController', function ($scope, $rootScope, $timeout, $location, $anchorScroll, $window, searchService) {

    //code

});

If I refresh the Results.html page how can I immediately redirect to the Home.html view instead of reloading the Results.html page?

EDIT:

I have updated the ResultsController.js with the following but this code does not seem to be called on a reload, it does not redirect to home.

var app = angular.module('app');

app.controller('ResultsController', function ($scope, $rootScope, $timeout, $location, $anchorScroll, $window, searchService) {

    $rootScope.$on('$locationChangeStart', function (event) {
        $state.go('/');
    });
});

Also to add I have added the following to the Web.config of my app to handle rerouting:

<rewrite>
  <rules>
    <rule name="AngularJS" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

Question Background:

I have encountered what seems to be a mon issue with AngularJS where I am implementing:

 $locationProvider.html5Mode({ enabled: true, requireBase: false });

This has removed the '#' from my URL routes and in-turn caused a 404 error. I fixed that by adjusting web.config and the page reloads now.

The Issue:

I have a 2 views called Home.html and Results.html. I also have a index.html which houses the in my app which houses a <div ui-view> where the home and result views are injected into.

The following is the routes used:

http://localhost:5XXXX/ - home
http://localhost:5XXXX/results - results

I want to redirect to the home.html view when results.html is refreshed. Currently when results.html is refreshed all of y scope data is lost along with jquery plugin object state in the directives.

The Code:

app.js - this houses the routes of the app:

 angular
  .module('app', [
    'ui.router',
    'ui.bootstrap',
    'ngAnimate',
    'ngResource',
    'rzModule',
    'angular-ladda',
    'jcs-autoValidate'
  ])
  .config(['$urlRouterProvider', '$stateProvider', '$locationProvider', function ($urlRouterProvider, $stateProvider, $locationProvider) {

      $urlRouterProvider.otherwise('/');
      $stateProvider
        .state('home', {
            url: '/',
            templateUrl: 'home.html',
            controller: 'HomeController'
        })


          $stateProvider.state('results', {
              url: '/results',
              templateUrl: 'Results.html',
              controller: 'ResultsController'
          })

          $locationProvider.html5Mode({
              enabled: true,
              requireBase: false
          });
    }]);

ResultsController.js:

var app = angular.module('app');

app.controller('ResultsController', function ($scope, $rootScope, $timeout, $location, $anchorScroll, $window, searchService) {

    //code

});

If I refresh the Results.html page how can I immediately redirect to the Home.html view instead of reloading the Results.html page?

EDIT:

I have updated the ResultsController.js with the following but this code does not seem to be called on a reload, it does not redirect to home.

var app = angular.module('app');

app.controller('ResultsController', function ($scope, $rootScope, $timeout, $location, $anchorScroll, $window, searchService) {

    $rootScope.$on('$locationChangeStart', function (event) {
        $state.go('/');
    });
});

Also to add I have added the following to the Web.config of my app to handle rerouting:

<rewrite>
  <rules>
    <rule name="AngularJS" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>
Share Improve this question edited Sep 13, 2016 at 23:52 user1352057 asked Sep 13, 2016 at 22:41 user1352057user1352057 3,1809 gold badges52 silver badges118 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

Use $locationChangeStart

$rootScope.$on('$locationChangeStart', function(event) {
   $state.go('/');
});

Proper code : $locationChangeStart run
Put this in main app file and attach it to app like app.config do it like app.run()

.run(['$rootScope', '$state', function($rootScope, $state, userService) {
       $rootScope.$on("$locationChangeStart", function(event, next, current) {
          if(next == current) {
                event.preventDefault();
                $state.transitionTo('/');
            }
       });
}]);

some pseudo code

var app = angular.module('app');
app.controller('ResultsController', function ($scope, $rootScope, $timeout, $location, $anchorScroll, $window, searchService, $state) {
  if (data unavailable) {
    $state.go('home');
  }
});

if your results data isn't available just do $state.go('home'); in the ResultsController

Looks like that's been answered here: How to redirect on different view on refresh in AngularJS?

$rootScope.$on("$locationChangeStart", function(event, next, current) { 

if(next==current && next=='/results')
    event.preventDefault();
    $state.go('home');
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170287a4614883.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信