我正在使用Angular UI路由器,想要重新加载当前状态并刷新所有数据/重新运行当前状态及其父状态的控制器。

我有3个状态级别:目录。组织。细节

organizations包含一个包含组织列表的表。单击表中的项目加载directory. organizations .details,并使用$StateParams传递该项的ID。所以在细节状态下,我加载这个项目的细节,编辑它们,然后保存数据。到目前为止一切正常。

现在我需要重新加载这个状态并刷新所有数据。

我试过:

 $state.transitionTo('directory.organisations');

它会回到父状态但不会重载控制器,我猜是因为路径没有改变。理想情况下,我只想留在directory. organizations .details状态,并刷新父目录中的所有数据。

我也试过:

$state.reload()

我在$state的API WIKI上看到过这个。重新加载”(错误与控制器重新验证现在,修复很快)。

如果有任何帮助,我将不胜感激。


当前回答

我有这个问题,它是破坏我的头,我的路由是从一个JSON文件读取,然后馈送到一个指令。我可以点击一个ui状态,但我不能重新加载页面。我的一个同事给我展示了一个很好的小技巧。

获取你的数据 在你的应用配置中创建一个加载状态 将您想要进入的状态保存在rootscope中。 在app.run中设置你的位置为“/loading” 在加载控制器中解析路由承诺,然后将位置设置为您的预期目标。

这对我很管用。

希望能有所帮助

其他回答

这个解决方案适用于AngularJS V.1.2.2:

$state.transitionTo($state.current, $stateParams, {
    reload: true,
    inherit: false,
    notify: true
});

对于angular v1.2.26,上面这些都不行。调用上述方法的ng-click必须单击两次,才能使状态重新加载。

所以我最终使用$timeout模拟2次点击。

$provide.decorator('$state',
        ["$delegate", "$stateParams", '$timeout', function ($delegate, $stateParams, $timeout) {
            $delegate.forceReload = function () {
                var reload = function () {
                    $delegate.transitionTo($delegate.current, angular.copy($stateParams), {
                        reload: true,
                        inherit: true,
                        notify: true
                    })
                };
                reload();
                $timeout(reload, 100);
            };
            return $delegate;
        }]);

你可以使用@Rohan回答https://stackoverflow.com/a/23609343/3297761

但如果你想让每个控制器在视图改变后重新加载,你可以使用

myApp.config(function($ionicConfigProvider) { $ionicConfigProvider.views.maxCache(0); ... }

愚蠢的变通方法总是有效的。

$state.go("otherState").then(function(){
     $state.go("wantedState")
});

离子骨架

$state.transitionTo($state.current, $state.$current.params, { reload: true, inherit: true, notify: true });//reload

$stateProvider.
  state('home', {
    url: '/',
    cache: false, //required

https://github.com/angular-ui/ui-router/issues/582