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

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

问题描述

根据文档,默认情况下,angularjs ui-router url参数是可选的.那么有没有办法创建强制性参数?就像参数丢失或为null一样,它将不会进入该页面?

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路由器解决,检查路由参数是否丢失.

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天全站免登陆