2016-10-04 7 views
0

角度1.5と角度uiルータでは、自分の州の戻るボタンの動作を変更して、州/地域の名前を認識するようにしたいのですが、url/restaurant/1?param1 = & param2 =ページを更新せずに動的に変化する値。私はバインディングとURLがプロパティの変更時に変更されていないが、私が戻るボタンを押すと、名前で$ rootScopeに保存された状態ではなく、以前のparams状態になる。私はこれを何時間もデバッグしていますが、現在のソリューションは時にはうまくいきます。なぜなら、urlの同期が常に呼び出されるわけではないため、URLが更新されても状態は更新されないからです。私は正しく戻るボタンがヒットしたときを正しく認識していますが、状態をリフレッシュしていません。今、私はlocation.assignを使用しなければなりませんが、それはいくつかの時間だけ動作します。とにかく、角度uiルータのdeferInterceptを再同期する必要はありますか?

function bind(propGetters, 
 
    getUrl) { 
 
    let unwatch = this._$rootScope.$watchGroup(propGetters,() => { 
 
    let trimmedUrl = getUrl(); 
 
    let remove = this._$rootScope.$on('$locationChangeSuccess',() => { 
 
     this._$rootScope.disableBackButton = false; 
 
     remove(); 
 
    }); 
 

 
    this._$rootScope.disableBackButton = true; 
 
    if (!this._$rootScope._hitBackButton) { 
 
     this._$rootScope.shouldSync = false; 
 
     this._$location.url(trimmedUrl); 
 
    } 
 
    }); 
 

 
    return { 
 
    unbind(): void { 
 
     unwatch(); 
 
    } 
 
    }; 
 
    module.run(
 
    ($urlRouter, $rootScope) => { 
 
     $rootScope.shouldSync = true; 
 

 
     $rootScope.$on('$locationChangeSuccess', e => { 
 
     if ($rootScope.shouldSync) { 
 
      $urlRouter.sync(); 
 
     } else if (!$rootScope._hitBackButton) { 
 
      e.preventDefault(); 
 
     } 
 
     $rootScope.shouldSync = true; 
 
     }); 
 

 
     $rootScope.$on("$stateChangeSuccess", (event, toState, toParams, fromState, fromParams) => { 
 
     if ($rootScope.previousState) { 
 
      if ($rootScope.previousState.name !== fromState.name && !$rootScope.disableBackButton) { 
 
      $rootScope.previousState = lodash.merge(fromState, { params: fromParams }); 
 
      console.log($rootScope.previousState.name, 'previousState'); 
 
      } 
 
     } else { 
 
      this._$rootScope.previousState = lodash.merge(fromState, { params: fromParams }); 
 
     } 
 
     }); 
 

 
     $rootScope.$on('$locationChangeSuccess', (evt, newUrl, oldUrl) => { 
 
     $rootScope.actualPrevious = oldUrl; 
 
     if ($rootScope._hitBackButton) { 
 
      this._urlProvider.sync(); 
 
      $rootScope._hitBackButton = false; 
 
     } 
 
     }); 
 
     $rootScope.$on('$locationChangeStart', (evt, newUrl, oldUrl) => { 
 
     if ($rootScope.actualPrevious === newUrl && $rootScope.previousState && !$rootScope.disableBackButton && !$rootScope._hitBackButton) { 
 
      $rootScope.shouldSync = true; 
 
      event.preventDefault(); 
 
      console.log('hit back', $rootScope.previousState.name); 
 
      $rootScope._hitBackButton = true; 
 
      window.location.assign(this._urlService.getFullPath(this._$state.href($rootScope.previousState.name, $rootScope.previousState.params))) 
 
      // this._$state.go($rootScope.previousState.name, $rootScope.previousState.params, { reload: true }); - this doesn't seem to always work because of watch collisions? 
 
     } 
 
     }); 
 
     $urlRouter.listen(); 
 
    });

+0

:バックボタンはdeferInterceptとプラスマイナスreloadOnSearch上記の私の解決策で正常に動作する必要があります)=> { $ urlRouterProvider.deferIntercept(); }); on config – evanjmg

+0

*私はお勧めします:あなたの時間をかけてプランナーを作成してください。より多くの視聴者を獲得し、助けを得る機会を増やすだけでなく、自分で解決策を見つけるのにも役立ちます* –

答えて

0

5時間遊ん後、 は、私はそれがすべて一緒にし、代わりにreloadOnSearch deferInterceptの使用を避けるのがベストですが見つかりました:あなたは意志ので

$stateProvider.state('app.restaurant', { url: '/restaurant/{slug}?param1&param2&param3', resolve: { param1: function 1, param2: function 2, param3: function 3 }, reloadOnSearch: false, params: { slug: { value: null }, }, controller: 'restaurantController', template: }); ノートのような偽paramsの代わりにslferにdeferInterceptを使用する必要があります。あなたのURLが変わったら、paramsを使うことを強くお勧めします。 angular.ui.IUrlRouterProvider:私は `module.config(($ urlRouterProviderを使用偽

0
Full modified implementation: 

this._$rootScope.$on('$locationChangeStart', (evt, newUrl, oldUrl) => { 
 
     if (this._$rootScope.actualPrevious === newUrl && this._$rootScope.previousState && !this._$rootScope._hitBackButton) { 
 
     evt.preventDefault(); 
 
     this._$rootScope._hitBackButton = true; 
 
     this._$state.go(this._$rootScope.previousState.name, this._$rootScope.previousState.params); 
 
     } 
 
    }); 
 
    this._$rootScope.$on("$stateChangeSuccess", (event, toState, toParams, fromState, fromParams) => { 
 
     if (this._$rootScope.previousState) { 
 
     if (this._$rootScope.previousState.name !== fromState.name) { 
 
      this._$rootScope.previousState = lodash.merge(fromState, { params: fromParams }); 
 
     } 
 
     } else { 
 
     this._$rootScope.previousState = lodash.merge(fromState, { params: fromParams }); 
 
     } 
 
    }); 
 
    this._$rootScope.$on('$locationChangeSuccess', (evt, newUrl, oldUrl) => { 
 
     this._$rootScope.actualPrevious = oldUrl; 
 
     if (this._$rootScope._hitBackButton) { 
 
     this._$rootScope._hitBackButton = false; 
 
     } 
 
    });

関連する問題