最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS routing to static file - Stack Overflow

programmeradmin7浏览0评论

I have a simple angularjs app, with ngRoute module for routing in html5Mode. How can I have a link to some static file on my page, and not to have it intercepted by angular routing module?

Here's the example:

HTML:

    <head>
        <base href='/'></base>
    </head>
    <body ng-app="crudApp">

    <a href="/">Home</a>
    <a href="/user">User</a>
    <a href="/users.html">users.html</a>

    <div ng-view></div>

JS routing:

$routeProvider
            .when('/', {
                templateUrl: 'app/ponents/home/homeView.html',
                controller: 'HomeController'

            })
            .when('/user', {
                templateUrl: 'app/ponents/user/userView.html',
                controller: 'UserController'

            })
            .otherwise({
                redirectTo: '/'
            });

When I click on User link I get routed to localhost:8080/user, and my controller and template work fine. When I click on users.html link I get routed to home, but I want to invoke a static home.html page.

I have a simple angularjs app, with ngRoute module for routing in html5Mode. How can I have a link to some static file on my page, and not to have it intercepted by angular routing module?

Here's the example:

HTML:

    <head>
        <base href='/'></base>
    </head>
    <body ng-app="crudApp">

    <a href="/">Home</a>
    <a href="/user">User</a>
    <a href="/users.html">users.html</a>

    <div ng-view></div>

JS routing:

$routeProvider
            .when('/', {
                templateUrl: 'app/ponents/home/homeView.html',
                controller: 'HomeController'

            })
            .when('/user', {
                templateUrl: 'app/ponents/user/userView.html',
                controller: 'UserController'

            })
            .otherwise({
                redirectTo: '/'
            });

When I click on User link I get routed to localhost:8080/user, and my controller and template work fine. When I click on users.html link I get routed to home, but I want to invoke a static home.html page.

Share Improve this question edited Jun 26, 2019 at 11:37 Al Fahad 2,5685 gold badges32 silver badges38 bronze badges asked Apr 15, 2015 at 13:59 D00deD00de 9183 gold badges8 silver badges26 bronze badges 2
  • try without the slash (or path to where ever your static file is ...) if you just want direct html... <a href="users.html">users.html</a> – vidriduch Commented Apr 15, 2015 at 14:04
  • why not create a route for home.html? – harishr Commented Apr 15, 2015 at 14:17
Add a ment  | 

1 Answer 1

Reset to default 18

From the AngularJS docs, you have 3 options:

Html link rewriting

(...)

In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.

  • Links that contain target element
    Example: <a href="/ext/link?a=b" target="_self">link</a>
  • Absolute links that go to a different domain
    Example: <a href="http://angularjs/">link</a>
  • Links starting with '/' that lead to a different base path
    Example: <a href="/not-my-base/link">link</a>

What you might be looking for is the first example:

<a href="/users.html" target="_self">users.html</a>
发布评论

评论列表(0)

  1. 暂无评论