Kri*_*ons 15 anchor angularjs angular-ui-router
更新的问题
标题中的问题仍然存在 - 是否可以捕获?parameters
和取消视图重新加载.
原始问题:我需要为我的应用程序添加锚点支持(主题的锚点).
这是我有多远:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
我的根本问题是当我点击链接时#/topics/slug-name?section=section-1
,状态被完全重新加载,然后重新请求主题的数据.如何只是"刷新"查询参数(并在控制器中捕获该事件)但不重新加载状态(以保持数据加载)?
编辑
对于我的问题,我没有注意到一个相当简单的解决方案 - 根据"Hashbang和HTML5模式"的AngularJS文档 - 最后一个哈希是可以访问的$location.hash()
,滚动可以由$anchorScroll()
所以我目前的解决方案是:
在控制器中:
$scope.$watch('$location.hash', function () {
_.defer($anchorScroll);
});
Run Code Online (Sandbox Code Playgroud)
需要延迟,因为内容可能尚未解析且没有id.
在模板中我只需要设置目标#/topics/slug-name#section-name
.
Jim*_*ane 20
我的两分钱
首先检查有关设置reloadOnSearch
为false的@doodeec答案:
{
route:'/',
resolve: {
templateUrl:'template.html',
reloadOnSearch: false
}
},
Run Code Online (Sandbox Code Playgroud)
然后在控制器上你要抓住这个:
/* Setup to detect when there is a change in the search */
$scope.$on('$routeUpdate', function(next, current) {
// Do something when routeupdate is called.
});
Run Code Online (Sandbox Code Playgroud)
我想改变搜索的情况:
$location.search({'slide_id': slide_id})
Run Code Online (Sandbox Code Playgroud)
还要小心一点:
如果您的路由参数不更改(或更改为相同的值),因为重新加载已禁用,那么您可能需要强制更改位置:
$scope.$emit('$locationChangeSuccess');
Run Code Online (Sandbox Code Playgroud)
来自评论:
如果ngRoute
没有$ routeUpdate事件.在这种情况下,你要听$scope.$on('$locationChangeSuccess', ...)
,而不是
doo*_*eec 13
您是否尝试过reloadOnSearch
路由定义中的参数?
{
route:'/',
resolve: {
templateUrl:'template.html',
reloadOnSearch: false
}
},
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13489 次 |
最近记录: |