Gih*_*han 50 angularjs angularjs-routing
<h1>{{header}}</h1>
<!-- This Back button has multiple option -->
<!-- In home page it will show menu -->
<!-- In other views it will show back link -->
<a ng-href="{{back.url}}">{{back.text}}</a>
<div ng-view></div>
Run Code Online (Sandbox Code Playgroud)
在我的模块配置中
$routeProvider.
when('/', {
controller:HomeCtrl,
templateUrl:'home.html'
}).
when('/menu', {
controller:MenuCtrl,
templateUrl:'menu.html'
}).
when('/items', {
controller:ItemsCtrl,
templateUrl:'items.html'
}).
otherwise({
redirectto:'/'
});
Run Code Online (Sandbox Code Playgroud)
控制器
function HomeCtrl($scope, $rootScope){
$rootScope.header = "Home";
$rootScope.back = {url:'#/menu', text:'Menu'};
}
function MenuCtrl($scope, $rootScope){
$rootScope.header = "Menu";
$rootScope.back = {url:'#/', text:'Back'};
}
function ItemsCtrl($scope, $rootScope){
$rootScope.header = "Items";
$rootScope.back = {url:'#/', text:'Back'};
}
Run Code Online (Sandbox Code Playgroud)
正如你在我的控制器中看到的那样,我已经硬编码了后退按钮的URL和文本(实际上我不需要使用图像的文本).通过这种方式,我发现在某些情况下后退按钮导航不正确.我无法使用history.back()后退按钮更改为主视图中的菜单链接.
所以我的问题是如何获得控制器中的先前路径路径或更好的方法来实现这一点?
我创建了一个Plunker演示我的问题.请检查一下.
and*_*rsh 74
该替代方案还提供后退功能.
模板:
<a ng-click='back()'>Back</a>
Run Code Online (Sandbox Code Playgroud)
模块:
myModule.run(function ($rootScope, $location) {
var history = [];
$rootScope.$on('$routeChangeSuccess', function() {
history.push($location.$$path);
});
$rootScope.back = function () {
var prevUrl = history.length > 1 ? history.splice(-2)[0] : "/";
$location.path(prevUrl);
};
});
Run Code Online (Sandbox Code Playgroud)
Mar*_*cok 22
使用$ locationChangeStart或$ locationChangeSuccess事件,第3个参数:
$scope.$on('$locationChangeStart',function(evt, absNewUrl, absOldUrl) {
console.log('start', evt, absNewUrl, absOldUrl);
});
$scope.$on('$locationChangeSuccess',function(evt, absNewUrl, absOldUrl) {
console.log('success', evt, absNewUrl, absOldUrl);
});
Run Code Online (Sandbox Code Playgroud)
Moh*_*far 16
在你的HTML中:
<a href="javascript:void(0);" ng-click="go_back()">Go Back</a>
Run Code Online (Sandbox Code Playgroud)
在您的主控制器上:
$scope.go_back = function() {
$window.history.back();
};
Run Code Online (Sandbox Code Playgroud)
当用户单击"返回"链接时,将调用控制器功能,它将返回上一个路径.
小智 5
@andresh对我来说locationChangeSuccess工作而不是routeChangeSuccess.
//Go back to the previous stage with this back() call
var history = [];
$rootScope.$on('$locationChangeSuccess', function() {
history.push($location.$$path);
});
$rootScope.back = function () {
var prevUrl = history.length > 1 ? history.splice(-2)[0] : "/";
$location.path(prevUrl);
history = []; //Delete history array after going back
};
Run Code Online (Sandbox Code Playgroud)
只是为了记录:
回调参数previousRoute有一个名为的属性,$route该属性与服务非常相似$route。不幸的是currentRoute,争论并没有太多关于当前路线的信息。
为了克服这个问题,我尝试过类似的事情。
$routeProvider.
when('/', {
controller:...,
templateUrl:'...',
routeName:"Home"
}).
when('/menu', {
controller:...,
templateUrl:'...',
routeName:"Site Menu"
})
Run Code Online (Sandbox Code Playgroud)
请注意,在上面的路由配置中routeName添加了一个名为的自定义属性。
app.run(function($rootScope, $route){
//Bind the `$routeChangeSuccess` event on the rootScope, so that we dont need to
//bind in induvidual controllers.
$rootScope.$on('$routeChangeSuccess', function(currentRoute, previousRoute) {
//This will give the custom property that we have defined while configuring the routes.
console.log($route.current.routeName)
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102638 次 |
| 最近记录: |