简体   繁体   English

使用 ui-router 的 AngularJS 路由

[英]AngularJS Routing using ui-router

Is it possible to have routing as follows in angular?是否可以按如下方式进行路由?
I'm using ui-router for routing, Angular version is 1.4 FYI我正在使用 ui-router 进行路由,Angular 版本是 1.4 仅供参考

.state('home', {
  url: '/:city',
  controller: 'HomeCtrl',
  templateUrl: 'templates/home.html'
}) 

.state('about', {
  url: '/about',
  controller: 'AboutCtrl',
  templateUrl: 'templates/about.html'
})

.state('login', {
  url: '/login',
  controller: 'LoginCtrl',
  templateUrl: 'templates/login.html'
})

When I tried this way angular is considering both /about and /login as home state and giving me "about" and "login" in state params is there anyway to override this to have urls as specified?当我尝试这种方式时,angular 将/about/login视为主状态,并在状态参数中给我"about""login" ,无论如何要覆盖它以具有指定的 urls 吗?

Just define your static routes before home state, here's a demo只需在home state 之前定义您的静态路由,这是一个演示

 var app = angular.module('app', ['ui.router']); app.config(function($stateProvider, $urlRouterProvider) { // what's the initial route? for the sake of example, it's `/almaty` $urlRouterProvider.otherwise("/almaty"); $stateProvider .state('about', { url: '/about', template: '<h1>about</h1>' }) .state('login', { url: '/login', template: '<h1>login</h1>' }) .state('home', { url: '/:city', controller: function($scope, $stateParams) { $scope.stateCity = $stateParams.city }, template: '<h1>city - {{stateCity}}</h1>' }) });
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://unpkg.com/angular-ui-router@0.4.2/release/angular-ui-router.min.js"></script> <div ng-app="app"> <ul> <li> <a href="#/almaty">city Almaty</a> </li> <li> <a href="#/about">about</a> </li> <li> <a href="#/login">login</a> </li> </ul> <div ui-view></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM