2014-08-27 45 views
8

Denetleyicimin yenilemeden tarayıcı url güncellemek için yeni deferIntercept() ui-yönlendirici kullanmak params URL'yi öncekine değiştirir, ancak kontrol cihazım durumunu bu değişikliği yansıtmak için güncelleyemiyorum. $ stateParams, kullanıcı sayfayı ilk kez yüklediğinde ayarlanan değerleri hala içerir.ui-yönlendirici deferIntercept ve devlet

Kullanıcı geri düğmeyi tıkladığında veya URL'yi el ile değiştirdiğinde, denetleyicimin içindeki $ state ve $ stateParams nesnelerini güncellemenin en iyi yolu nedir?

Teşekkürler!

cevap

7

$urlRouter.listen() numaralı aramanız, olay işleyicisinin dışına yerleştirilmelidir.

$rootScope.$on('$locationChangeSuccess', function(e, newUrl, oldUrl) { 
    e.preventDefault(); 
    if ($state.current.name !== 'search') { 
    $urlRouter.sync(); 
    } 
}); 

// Moved out of listener function 
$urlRouter.listen(); 

Kaynak: Kod sağlanan sen şekilde değiştirilmelidir snippet'ineofficial documentation for $urlRouterdeferIntercept yöntemi için bir kod örneği sağlar. Bu dinleyici işlevinin $urlRouter.listen() dışarıdan aramayı gerçekleştirir:

`$ state` parametre ancak
+0

; 'Götürmezse Özellikle senkronizasyonu aramadıkça rotayı senkronize etmeyin – Shamoon

+0

i if (UserService.isLoggedIn()) dönüş' çalıştırdığınızda ben buluyorum güncellenmiş değil kalır

var app = angular.module('app', ['ui.router.router']); app.config(function ($urlRouterProvider) { // Prevent $urlRouter from automatically intercepting URL changes; // this allows you to configure custom behavior in between // location changes and route synchronization: $urlRouterProvider.deferIntercept(); }).run(function ($rootScope, $urlRouter, UserService) { $rootScope.$on('$locationChangeSuccess', function(e) { // UserService is an example service for managing user state if (UserService.isLoggedIn()) return; // Prevent $urlRouter's default handler from firing e.preventDefault(); UserService.handleLogin().then(function() { // Once the user has logged in, sync the current URL // to the router: $urlRouter.sync(); }); }); // Configures $urlRouter's listener *after* your custom listener $urlRouter.listen(); }); 
Maruf

İlgili konular