Dot notation in href replacing entire URL path

时间:2016-10-09 15:54:50

标签: javascript html angularjs href

I am working on an AngularJS web application where I need to use an href in the template to launch a child state. Long story, but href is my only option for now (not alternatives like $state.go() or ui-sref directives, etc.).

Basically I'm trying to launch a child state (a modal window) that is routed to my current state (parent). An example current URL from which the link can be clicked is

localhost:3000/#/contact/aboutus

This is not the only possible parenting state throughout the app, though, so I need the initial part of the path to remain dynamic. Let's say I'm trying to launch a "directory" modal window from this page, though, that corresponds to the following URL:

localhost:3000/#/contact/aboutus/directory

I would expect this to be possible with the following code that I have:

<div class="internal-margin">
    <a href="./directory">
        <span ng-bind-html="'{{link.label | translate}}'"></span>
    </a>
</div>

Notice the href="./directory". I am trying to use this dot notation in order to append to the existing URL path. When I click the link on the page, however, it directs me to the URL:

localhost:3000/directory

The entire path is replaced and this URL is a 404. Why is this happening? Does the problem lie deeper in how the ui routing is taking place, or am I completely missing something?

1 个答案:

答案 0 :(得分:1)

您的解释并未考虑您使用基于散列的路由

./directory不会附加到url哈希,并且没有哈希

手动编写href时,必须是:

<a href="#/contact/aboutus/directory">

没有简写