运行功能和位置路径问题

时间:2016-12-23 13:14:04

标签: angularjs

我正在使用run函数来有条件地将人们重定向到页面,关于他们的登录状态。重定向取决于用户状态工作得很好,但重定向后,页面之间的导航将无法工作,当我点击导航我留在同一页面。这就是我的代码看起来,任何人都可以帮助我吗?

var app = angular.module( 'YourApp', [ 'ngMaterial', "ngRoute" ])
app.config(($routeProvider, $mdThemingProvider, $mdIconProvider, $mdDateLocaleProvider) => {

  $mdIconProvider
    .iconSet("lala", 'assets/logo/ic_keyboard_arrow_left_white_48px.svg', 24)
    .iconSet("social", 'img/icons/sets/social-icons.svg', 24);

  $mdThemingProvider.theme('primary')
      .primaryPalette('brown')
      .accentPalette('yellow');

      $mdDateLocaleProvider.formatDate = function(date) {
    return moment(date).format('DD-MM-YYYY');
 };


  $routeProvider
  .when("/", {
    templateUrl: "home.html",
    controller: "myCtrl"
  }).when("/voyages", {
    templateUrl: "voyages.html",
    controller: "voyageCtrl"
  }).when("/bagages", {
    templateUrl: "bagages.html",
    controller: "BagagesCtrl"
  }).when("/login",{
    templateUrl: "login.html",
    controller: "loginCrtl"
  }).when("/voyageID/:voyageid" , {
    templateUrl: "voyageID.html",
    controller: "voyageIDCtrl"
  })
  .otherwise({
   redirectTo: '/'
});

})

.run(function($rootScope, $location) {
  var Parse = require('parse');
  Parse.initialize("CODE", "PIN");
  Parse.serverURL = 'https://stelimac.herokuapp.com/parse';
    $rootScope.$on("$routeChangeStart", (event) => {
      var currentUser = Parse.User.current();
        if (!currentUser) {
          $location.path("/login")
        } else {
         $location.path("/")
        }
    })
});

1 个答案:

答案 0 :(得分:2)

将$ location.path更改为$ state.go

.run(function($rootScope, $state) {
      var Parse = require('parse');
      Parse.initialize("CODE", "PIN");
      Parse.serverURL = 'https://stelimac.herokuapp.com/parse';
        $rootScope.$on("$routeChangeStart", (event) => {
          var currentUser = Parse.User.current();
            if (!currentUser) {
              $state.go("/login")
            } else {
             $state.go("/")
            }
        })
    });