想了解angularjs–AngularUI路由器–强制URL参数?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于angularjs路由拦截的相关问题,此外,我们还将为您介绍关于Angul
想了解angularjs – Angular UI路由器 – 强制URL参数?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于angularjs路由拦截的相关问题,此外,我们还将为您介绍关于AngularJs UI路由器 – 一个具有多个URL的状态、AngularJS UI路由器 – 更改url没有重新加载状态、AngularJS UI路由器-更改URL而不重新加载状态、AngularJS UI路由器URL将消失的新知识。
本文目录一览:- angularjs – Angular UI路由器 – 强制URL参数?(angularjs路由拦截)
- AngularJs UI路由器 – 一个具有多个URL的状态
- AngularJS UI路由器 – 更改url没有重新加载状态
- AngularJS UI路由器-更改URL而不重新加载状态
- AngularJS UI路由器URL将消失
angularjs – Angular UI路由器 – 强制URL参数?(angularjs路由拦截)
例:
我有2个观点或状态,列表和单个.它们共享同一个控制器.我的路线映射如下:
state: app.list url: /list controller: appCtrl state: app.single url: /single/:id controller: appCtrl
现在,我希望只有在指定了id时才能访问single,否则只需重定向到其他页面.怎么可能使用同一个控制器?
解决方法
你可以使用$urlRouterProvider和when()进行重定向.
app.config(function($urlRouterProvider){ // when there is an single route without param,redirect to /list $urlRouterProvider.when('/single/:id',['$match','$state',function($match,$state) { if($match.id === ""){ $state.transitionTo("app.list"); } } ]); });
工作演示:http://plnkr.co/edit/sEoUGGCEge0XbKp3nQnc?p=preview
方法2
您可以检查控制器端的参数并将其重定向到特定页面
myApp.controller('MainCtrl',function($state,$stateParams) { if($state.current.name == 'app.single' && $stateParams.id === ""){ $state.transitionTo("app.list"); } });
工作演示:http://plnkr.co/edit/QNF1RHy4Prde4CRhNLFa?p=preview
注意:在上面的演示中,当您的当前状态不应该是app.single时,重定向会起作用.如果你处于app.single状态并尝试没有param,则状态不会改变.所以转到主页面,然后单击没有单个状态参数的链接.它将重定向到列表状态.
AngularJs UI路由器 – 一个具有多个URL的状态
例如,这是已经存在的
.state('site.link1',{ url: '/link1',templateUrl: '/views/link1.html',controller: 'link1Ctrl' })
并且请求添加www.site.com/newlink指向link1页面。有这样的东西吗?
.state('site.link1',{ url: '/link1,/newlink',...
https://github.com/angular-ui/ui-router/wiki/URL-Routing
.state('site.link',{ url: '/{link}' .. }
所以当你使用相同的状态,像这样
$state.go('site.link',{link: 'link1'}) $state.go('site.link',{link: 'link2'})
AngularJS UI路由器 – 更改url没有重新加载状态
services.service('$locationEx',['$location','$route','$rootScope',function($location,$route,$rootScope) { $location.skipReload = function () { var lastRoute = $route.current; var un = $rootScope.$on('$locationChangeSuccess',function () { $route.current = lastRoute; un(); }); return $location; }; return $location; }]);
和控制器
$locationEx.skipReload().path("/category/" + $scope.model.id).replace();
我正在考虑用ui-router替换routeProvider用于嵌套路由,但是在ui-router中找不到。
是可能的 – 对angular-ui路由器做同样吗?
为什么我需要这个?
让我用一个例子解释:
创建新类别的路径是/ category / new
单击保存我显示成功警报,我想要更改路线/类别/新到/ caterogy / 23(23 – 是存储在db中的新项目的ID)
$state.go('.detail',{id: newId})
可以替换
$state.transitionTo('.detail',{id: newId},{ location: true,inherit: true,relative: $state.$current,notify: false })
编辑:如fracz建议,它可以简单地是:
$state.go('.detail',{notify: false})
AngularJS UI路由器-更改URL而不重新加载状态
当前,我们的项目正在使用default $routeProvider
,而我正在使用此“ hack”来进行更改,url
而无需重新加载页面:
services.service(''$locationEx'', [''$location'', ''$route'', ''$rootScope'', function($location, $route, $rootScope) { $location.skipReload = function () { var lastRoute = $route.current; var un = $rootScope.$on(''$locationChangeSuccess'', function () { $route.current = lastRoute; un(); }); return $location; }; return $location;}]);
和在 controller
$locationEx.skipReload().path("/category/" + $scope.model.id).replace();
我想到的替换routeProvider
用ui-router
筑巢路线,但无法找到这ui-router
。
有可能angular-ui-router
吗?
我为什么需要这个?让我用一个例子来解释:
创建新类别的路线是在SAVE /category/new
之后clicking
显示的success-alert
,我想将路线更改/category/new
为/caterogy/23
(23-是存储在db中的新项目的id)
答案1
小编典典只需使用即可$state.transitionTo
代替 $state.go
。内部$state.go
调用,$state.transitionTo
但会自动将选项设置为{ location: true, inherit: true, relative:$state.$current, notify: true }
。您可以呼叫$state.transitionTo
和设置notify:false
。例如:
$state.go(''.detail'', {id: newId})
可以替换为
$state.transitionTo(''.detail'', {id: newId}, { location: true, inherit: true, relative: $state.$current, notify: false})
编辑。
$state.go(''.detail'', {id: newId}, {notify: false})
AngularJS UI路由器URL将消失
如何解决AngularJS UI路由器URL将消失?
我在项目中使用了ui-router,但遇到一个问题,当我单击链接按钮时,URL为http://localhost/Digital/#/Device/Device
。
但是当我再次单击该按钮时,URL将会像http://localhost/Digital/#
一样更改。
那我该如何解决这个错误?
这是我的代码:
routerapp.config([''$stateProvider'',function($stateProvider){
$stateProvider.state(''login'',{
url: ''/login'',views: {
'''': {
templateUrl: ''page/login/login.html'',controller: ''''
}
}
}).state(''device'',{
url:''/device'',views:{
'''':{
templateUrl:''page/main/home.html'',controller: ''''
},''header@device'':{
templateUrl: ''page/partials/header.html'',''menu@device'':{
templateUrl: ''page/partials/menu.html'',''main@device'':{
templateUrl: ''page/device/device.html'',controller: ''''
}
}
}).state(''device.show'',views:{
''table@device'':{
templateUrl:''page/partials/content.html'',controller:''''
}
}
})}])
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
关于angularjs – Angular UI路由器 – 强制URL参数?和angularjs路由拦截的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于AngularJs UI路由器 – 一个具有多个URL的状态、AngularJS UI路由器 – 更改url没有重新加载状态、AngularJS UI路由器-更改URL而不重新加载状态、AngularJS UI路由器URL将消失等相关内容,可以在本站寻找。
本文标签: