角度路由,直接URL导航 [英] Angular-routing, direct URL navigation

查看:91
本文介绍了角度路由,直接URL导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在index.html文件中定义了一个角度应用程序. 使用角度路由,我正在路由名为/erez的链接以使用模板加载视图.它在应用程序内部运行-当我从index.html上的导航栏单击指向/erez的链接时,它可以正常工作. 但是,当我直接转到地址栏上的my.site.com/erez时,它会显示404.我知道为什么没有服务器端代码,但是有没有一种纯粹的角度方法可以实现直接url? 我的路由代码:

I have an angular app defined on my index.html file. Using angular-routing i am routing a link named /erez to load a view with a template. It's working inside the app - when I click the link to /erez from the navbar on index.html it works perfectly. But when I go directly to my.site.com/erez on the address bar, it gives 404. I understand why that is, having no server-side code, but is there a pure angular way of achieving direct urls? my routing code:

var app = angular.module('labApp', ['ngRoute', 'angular.filter']);

app.config(function ($routeProvider, $locationProvider) {
    $routeProvider.
        when('/', {
            templateUrl: 'index.html',
            controller: 'mainCtrl'
        }).
        when('/erez', {
            templateUrl: 'erez2.html',
            controller: 'erezCtrl'
        }).
        otherwise({
            redirectTo: '/'
        });

    $locationProvider.html5Mode(true);
});

推荐答案

您可以从因此,您需要在标签内添加以下基本url标签:

So either you need this base url tag inside the tag:

<base href="/" />

但是请注意,此基本标记可能会破坏代码中的相对链接.或者,您可以使用:

But be aware, this base tag may break the relative links in your code. Alternatively you can use:

$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

希望这会有所帮助.

这篇关于角度路由,直接URL导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆