AngularJS ui 路由器必选参数 [英] AngularJS ui router mandatory parameters

查看:29
本文介绍了AngularJS ui 路由器必选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,angularjs ui-router url 参数默认是可选的.那么有没有办法创建强制参数呢?比如当参数缺失或者为空时就不会进入页面?

Based on the documentation, angularjs ui-router url parameters are by default optional. So is there a way to create mandatory parameters? Like when the parameter is missing or null it will not proceed to the page?

希望你能帮助我.

谢谢

推荐答案

使用 UI 路由器 resolve 检查路由参数是否丢失.

Use UI Routers resolve to check if route params are missing.

//Example of a single route
.state('dashboard', {
  url: '/dashboard/:userId',
  templateUrl: 'dashboard.html',
  controller: 'DashboardController',
  resolve: function($stateParams, $location){

    //Check if url parameter is missing.
    if ($stateParams.userId === undefined) {
      //Do something such as navigating to a different page.
      $location.path('/somewhere/else');
    }
  }
})

这篇关于AngularJS ui 路由器必选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆