【问题标题】:Is there way to reload specific ui-view with angular-ui-router?有没有办法用angular-ui-router重新加载特定的ui-view?
【发布时间】:2015-03-20 02:14:45
【问题描述】:

这是我的测试应用程序http://plnkr.co/edit/OjpZ1h?p=preview

我有 404 错误页面状态,如果此页面不存在,我需要显示请求页面的路径。这里的重定向代码:

$urlRouterProvider.otherwise(function($injector, $location){
    var lastPath = $location.path();
    $injector.invoke(['$state', function($state) {
        var params = angular.copy($state.params);
        $state.go('main.error404',params, {location: false});
    }]);
    return lastPath;
});

除了应用程序已经处于错误状态并且我要转到也不存在的下一页的情况外,一切正常。

在这种情况下,不会发生重新加载,而是显示最后一页的路径。如果我将此代码更改为

$state.go('main.error404',params, {reload: true,location: false});

然后显示正确的路径,但所有视图都在重新加载,动画看起来很讨厌。

有没有办法用 angular-ui-router 重新加载特定的 ui-view?

【问题讨论】:

  • path 参数添加到main.error404 状态。这将导致在参数更改时重新加载状态

标签: angularjs angular-ui-router


【解决方案1】:

感谢 Chris T 的评论,我改写成这样:

   $stateProvider
   .state('main.error404',{
      url: '/404/{path}',
      views: {
        "page@": {
          templateUrl: "error.html",
          controller: function($scope,$location,$state){
            $scope.page = $state.params.path;
          }
        }
      }
    });

    $urlRouterProvider.otherwise(function($injector, $location){
        var lastPath = $location.path();
        $injector.invoke(['$state',function($state) {
            var params = angular.copy($state.params);
            params.path = lastPath;
            $state.go('main.error404',params, {location: false});
        }]);
        return lastPath;
    });

每次路径参数更改时都会重新加载状态

【讨论】:

    猜你喜欢
    • 2014-11-18
    • 2014-10-10
    • 1970-01-01
    • 2013-12-08
    • 2016-02-08
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多