remove exclamation from routing of state url Mean.io

末鹿安然 提交于 2019-12-25 09:18:13

问题


I want to remove exclamation marks from url state routing like my url is now http://localhost:3000/#!/auth/register

i just want to remove this "!" marks from url after "#"

Is it possible to do? with mean.io here is my app.js/system.js

'use strict';

//Setting up route

  angular.module('mean').config(['$stateProvider', '$urlRouterProvider',
  function($stateProvider, $urlRouterProvider) {
    // For unmatched routes:
    //$urlRouterProvider.otherwise('/');


    var checkLoggedin = function($q, $timeout, $http, $location) {
      // Initialize a new promise
      var deferred = $q.defer();

      // Make an AJAX call to check if the user is logged in
      $http.get('/loggedin').success(function(user) {
        // Authenticated

        if (user !== '0') $timeout(deferred.resolve);

        // Not Authenticated
        else {
          $timeout(deferred.reject);
          $location.url('/auth/login');
        }
      });

      return deferred.promise;
    };


    //  console.log($stateProvider);
    // states for my app
    $stateProvider
      .state('tasks', {
        url: '/kanban/:projectId/:projectSlug',
        templateUrl: 'system/views/index.html',
          controller: 'IndexController',
          resolve: {
          loggedin: checkLoggedin,
          onEnter: function($stateParams,$state, $uibModal) {

              if ( $stateParams.projectId != "" ) {

                  updateTopMenu('Kanban','task','#!/kanban/'+$stateParams.projectId+'/'+$stateParams.projectSlug);
                  updateTopMenu('Schedule','schedule','#!/schedule');
              }
          }
        }

        }).state('home',{

            url:'/',
            templateUrl: 'projects/views/index.html',
            controller: 'ProjectController',
            resolve:{
                        loggedin: checkLoggedin
            }
        }).state('taskEdit',{

            url:'/kanban/:projectId/:projectSlug/:taskSlug',
            templateUrl: 'system/views/index.html',
            controller: 'IndexController',
            resolve:{
                        loggedin: checkLoggedin
            }

        }).state('taskAdd',{

          url: "/task/taskAdd",
          onEnter: function($stateParams, $state, $uibModal) {
              $uibModal.open({
                  templateUrl: "system/views/include/model.html",
                  resolve: {},
                  controller: function($scope, $state, itemService) {
                                /*
                                $scope.state = $state.current;
                                $scope.params = $stateParams;
                                $scope.item = itemService.get($stateParams.id);
                                */
                                $scope.ok = function () {
                                    $scope.$close('clicked ok');
                                };

                                $scope.dismiss = function () {
                                    $scope.$dismiss('clicked cancel');
                                };
                            }
                    }).result.then(function (result) {
                        // $scope.$close
                        alert('result ->' + result);
                    }, function (result) {
                        // $scope.$dismiss
                        return $state.transitionTo("home");
                        alert('dismiss ->' + result);
                    }).finally(function () {
                        // handle finally
                        return $state.transitionTo("tasks");
                    });
                }
          });
  }
]).config(['$locationProvider',
  function($locationProvider) {
    $locationProvider.hashPrefix('!');
  }
]);

回答1:


You can make some transformation on url with $urlRouterProvider.rule in your config function like this:

$urlRouterProvider.rule(function ($injector, $location) {
    var path = $location.path(),
        normalized = path.replace('!/', '');
        if (path !== normalized) {
            return normalized;
        }
});

Not sure if this is the best way but it seems to work.

Inspired from: https://ui-router.github.io/docs/1.0.0-alpha.5/classes/url.urlrouterprovider.html



来源:https://stackoverflow.com/questions/41047355/remove-exclamation-from-routing-of-state-url-mean-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!