Angular $routeProvider: Why does CRUD new route not work?

徘徊边缘 提交于 2019-12-06 20:29:31

The $routeProvider try to test url pattern defined from TOP to BOTTOM.
So the URL pattern /users/new match /users/:id defined 3rd before /users/new defined 5th.

If you define /users/new before /users/:id, I expect that it would work properly.

The code should be like below.

App.config [
  '$routeProvider'
  '$locationProvider'

  ($routeProvider, $locationProvider, config) ->
    $routeProvider
      .when('/',
        templateUrl: '/partials/home.html'
      ).when('/users',
        templateUrl: 'partials/users/index.html'
        controller: 'UserIndexCtrl'
      # you should write it before '/users/:id'
      ).when('/users/new',
        templateUrl: 'partials/users/new.html'
        controller: 'UserNewCtrl'
      ).when('/users/:id',
        templateUrl: 'partials/users/show.html'
        controller: 'UserShowCtrl'
      ).when('/users/:id/edit',
        templateUrl: 'partials/users/edit.html'
        controller: 'UserEditCtrl'
      ).when('/new',
        templateUrl: 'partials/users/new.html'
        controller: 'UserNewCtrl'
      ).otherwise(redirectTo: '/')
    $locationProvider.html5Mode(true)
]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!