微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – 基于ui-router的查询重新加载查询参数更改

更新问题

标题中的问题仍然存在 – 是否可以捕获?参数并取消视图重新加载.

原来的问题:
我需要为我的应用程序添加锚点支持(锚点到主题).

这是我有多远:

angular.module('app.topics',[
    'ui.state','ui.router','restangular'
    'app.topics.controllers'
])
.config(function config($stateProvider) {
    $stateProvider.state('viewtopic',{
        url: '/topics/:slug?section',onEnter: function($stateParams) {
            // Works,as state has been reloaded.
            console.log('$stateParams',$stateParams);
        },resolve: {
            topic: function ($stateParams,Topic) {
                // Wrapper around Restangular. Returns promise.
                return new Topic().get($stateParams.slug);
            }
        },views: {
            'main': {
                controller: 'TopicCtrl',templateUrl: 'topics/templates/details.tpl.html'
            },'sidebar': {
                controller: 'SidebarCtrl',templateUrl: 'topics/templates/sidebar.tpl.html'
            }
        }
    });
});

angular.module('app.topics.controllers',[])
    .controller('TopicCtrl',function ($scope,topic,$rootScope,$location,$anchorScroll,$stateParams) {
        $scope.topic = topic;
        $scope.slug = $stateParams.slug;
        $scope.section = $stateParams.section;
        // ..

        $scope.$watch('$stateParams.section',function (newValue,newValue) {
            // Does not work.
            console.log('stateParams.section changed',newValue,newValue);
        });
    });

我的根本问题是当我点击链接#/ topics / slug-name?section = section-1,状态被完全重新加载,然后重新请求Topic的数据.如何才能“刷新”查询参数(并在控制器中捕获该事件),但是不要重新加载状态(保持数据加载)?

编辑

对于我的问题,有一个相当简单的解决方案,我没有注意到 –
每个AngularJS documentation在“Hashbang和HTML5模式” – 最后一个哈希可以通过$location.hash()访问,滚动可以由$anchorScroll()发起,

所以我目前的解决方案是:

在控制器中:

$scope.$watch('$location.hash',function () {
    _.defer($anchorScroll);
});

需要延迟,因为内容可能尚未被解析,并且没有id.

在模板中,我只需要将目标设置为#/ topics / slug-name#section-name.

你在路由定义中尝试过reloadOnSearch参数吗?
{
    route:'/',resolve: {
        templateUrl:'template.html',reloadOnSearch: false
    }
},

原文地址:https://www.jb51.cc/angularjs/143015.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐