How to prevent redirecting in Angular Js1.2

后端 未结 9 2046
情歌与酒
情歌与酒 2020-12-24 02:09

I was using AngularJs-1.0.7 and Bootstrap in my application. Recently I migrated from AngularJs-1.0.7 to AngularJs-1.2. I am using Bootstrap\'s Accordions and Tabs.

9条回答
  •  半阙折子戏
    2020-12-24 02:47

    credits to https://prerender.io/js-seo/angularjs-seo-get-your-site-indexed-and-to-the-top-of-the-search-results/

    My solution: - inside app.js try adding "$locationProvider.hashPrefix('!');"

        .config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider',
        function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) {
        $stateProvider
        .state('menu', {
            url: '/',
            cache: false,
            abstract: true,
            templateUrl: 'static/www/templates/menu.html'
        })
        .state('menu.main_page', {
        url: 'app/main',
        cache: false,
        views: {
            'menuContent': {
              templateUrl: 'static/www/templates/mainpage.html',
              controller: 'MainCtrl'
            }
        }
        })
        $locationProvider.hashPrefix('!');
        $urlRouterProvider.otherwise(function ($injector, $location) {
            var $state = $injector.get('$state');
            $state.go('menu.mainpage');
        });
       }])
    
    • inside index.html try adding

    and as Marcel said above

    .directive('a', function () {
    return {
        restrict: 'E',
        link: function (scope, elem, attrs) {
            if (attrs.href && attrs.href.indexOf('#') > -1) {
                elem.on('click', function (e) {
                    e.preventDefault();
                });
            }
        }
    };
    })
    

    that's all! works for me!

提交回复
热议问题