javascript - Angular 用户界面路由器状态问题

标签 javascript angularjs angular-ui-router

我目前正在尝试在我的 Angular 应用程序中设置路由。 我有这些状态:

.state('pet', {
  url: '/pet/:petId',
  templateUrl: 'scripts/views/profile/profile.html',
  controller: 'ProfileCtrl'
})
.state('addPet', {
  url: '/pet/add',
  templateUrl: 'scripts/views/profile/edit.html',
  controller: 'ProfileFormCtrl'
})
.state('editPet', {
  url: '/pet/:petId/edit',
  templateUrl: 'scripts/views/profile/edit.html',
  controller: 'ProfileFormCtrl'
})

问题是当我想继续“addPet”状态时,angular 试图调用“pet”状态并将“add”作为“:petId”参数。

所以我尝试用不同的方式来做:

.state('pet', {
  url: '/pet',
  abstract: true
})
.state('pet.show', {
  url: '/:petId',
  templateUrl: 'scripts/views/profile/profile.html',
  controller: 'ProfileCtrl'
})
.state('pet.add', {
  url: '/add',
  templateUrl: 'scripts/views/profile/edit.html',
  controller: 'ProfileFormCtrl'
})
.state('pet.edit', {
  url: '/:petId/edit',
  templateUrl: 'scripts/views/profile/edit.html',
  controller: 'ProfileFormCtrl'
})

它根本不起作用。

有什么建议吗?

最佳答案

改变顺序,保持addPet状态在pet状态

.state('addPet', {
  url: '/pet/add',
  templateUrl: 'scripts/views/profile/edit.html',
  controller: 'ProfileFormCtrl'
})
.state('pet', {
  url: '/pet/:petId',
  templateUrl: 'scripts/views/profile/profile.html',
  controller: 'ProfileCtrl'
})
.state('editPet', {
  url: '/pet/:petId/edit',
  templateUrl: 'scripts/views/profile/edit.html',
  controller: 'ProfileFormCtrl'
})

关于javascript - Angular 用户界面路由器状态问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31016444/

相关文章:

javascript - Angular JS - 使用 Angular JS 指令时多个 ajax 调用会在模糊时触发

javascript - 一个 ui-sref 链接有效,其余无效

javascript - AngularJS 中的展开折叠按钮

javascript - 使用授权 header 时出现 CORS header 错误

javascript - 在javascript中使用onClick获取被点击按钮的id

javascript - jQuery/JavaScript : Event handlers only assigned during the last iteration of a loop

javascript - ng-model 未定义,多种方式无法从文本框中获取文本

javascript - 在 Angular JS 中创建表,数据不显示

javascript - Controller 不应在 $location.path() 上重新加载

angularjs - Ionic 未反射(reflect)父范围的更改