当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - Why is ui-sref='tango::new' sending a request to /tangos/new (plural)?

问题描述:

HTML

<a ui-sref='tango::new'>Create Tango</a>

tango.routes.js

angular

.module('tango')

.config(config)

;

function config($stateProvider) {

$stateProvider

.state('tango', {

url: '/tango/:id',

templateUrl: '/states/tangos/tango.html',

controller: 'TangoController as vm'

})

.state('tango::new', {

url: '/tango/new',

templateUrl: '/states/tangos/tango.html',

controller: 'TangoController as vm'

})

;

}

This is the request that is going out when I click the link:

GET http://localhost:3000/tangos/new

Why is this? How can I stop it from happening?

网友答案:

There is a working plunker

The point here (despite of a bit unusual state name tango::new - which is still absolutely valid) is: the order of state definition:

the first (sooner) must be defined more specific, the second (later) the more generic

So, this should be the proper order:

.state('tango::new', {
  url: '/tango/new',
  templateUrl: 'states/tangos/tango.html',
  controller: 'TangoController as vm'
})

.state('tango', {
  url: '/tango/:id',
  templateUrl: 'states/tangos/tango.html',
  controller: 'TangoController as vm'
})

With this order in place, these links will work:

//ui-sref
<a ui-sref='tango({id:1})'>tango({id:1})
<a ui-sref='tango({id:22})'>tango({id:22})
<a ui-sref='tango::new'>Create Tango

//href
<a href="#/tango/333">
<a href="#/tango/4444">
<a href="#/tango/new">

Check it here

分享给朋友:
您可能感兴趣的文章:
随机阅读: