GVKun编程网logo

angularjs – Angular UI路由器 – 强制URL参数?(angularjs路由拦截)

22

想了解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 – Angular UI路由器 – 强制URL参数?(angularjs路由拦截)

我有几个状态使用相同的控制器.其中一些不需要URL参数,而有些则需要.如果未提供URL参数,如何避免可以访问状态?

例:

我有2个观点或状态,列表和单个.它们共享同一个控制器.我的路线映射如下:

state: app.list
url: /list
controller: appCtrl

state: app.single
url: /single/:id
controller: appCtrl

现在,我希望只有在指定了id时才能访问single,否则只需重定向到其他页面.怎么可能使用同一个控制器?

解决方法

方法1

你可以使用$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的状态

AngularJs UI路由器 – 一个具有多个URL的状态

我有一个请求添加另一个URL参数,指向我已经设置的状态。为了效率的目的,我试图看看我是否可以添加多个URL指向相同的状态,或者我应该使用$ UrlRouterProvider.when()方法重定向到这种新的情况下的状态。

例如,这是已经存在的

.state('site.link1',{
    url: '/link1',templateUrl: '/views/link1.html',controller: 'link1Ctrl'
  })

并且请求添加www.site.com/newlink指向link1页面。有这样的东西吗?

.state('site.link1',{
    url: '/link1,/newlink',...
你使用params:

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没有重新加载状态

AngularJS UI路由器 – 更改url没有重新加载状态

目前我们的项目使用默认$ 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;
}]);

和控制器

$locationEx.skipReload().path("/category/" + $scope.model.id).replace();

我正在考虑用ui-router替换routeProvider用于嵌套路由,但是在ui-router中找不到。

是可能的 – 对angular-ui路由器做同样吗?

为什么我需要这个?
让我用一个例子解释:
创建新类别的路径是/ category / new
单击保存我显示成功警报,我想要更改路线/类别/新到/ caterogy / 23(23 – 是存储在db中的新项目的ID)

简单地说,你可以使用$ state.transitionTo而不是$ state.go。 $ state.go在内部调用$ state.transitionTo,但自动将选项设置为{location:true,inherit:true,relative:$ state。$ current,notify:true}。您可以调用$ state.transitionTo并设置位置:false。例如:
$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而不重新加载状态

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();

我想到的替换routeProviderui-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将消失

如何解决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将消失等相关内容,可以在本站寻找。

本文标签: