$ locationProvider是不是在我的角度网页工作 [英] $locationProvider isn't working in my Angular page

查看:150
本文介绍了$ locationProvider是不是在我的角度网页工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角度页我使用 $ locationProvider 。但它不是在我的网页的工作。我在控制台这样遇到错误,的在HTML5模式$位置需要一个标签是present!的。所以我添加一个<基本href =//> ,也不能在我的角度网页的工作。在这里,我提到我的目录结构和我的code。

下面我样本项目链接了。帮我解决这个问题。

我的目录结构

  IWA(根目录下)
  > JS
      > LIB
          - > angular.js
          - > angular.route.js
       - > app.js
  >意见
       - > home.html做为
       - > about.html
  - >的index.html

的index.html

 <!DOCTYPE HTML>
 < HTML NG-应用=inQueueWeb>
 <头LANG =ENGT&;
     <间的charset =UTF-8>
     <脚本SRC =JS / lib目录/ angular.js>< / SCRIPT>
     <脚本SRC =JS / lib目录/角route.js>< / SCRIPT>
  < /头>
 <身体GT;
     < D​​IV NG-视图>< / DIV>
 < /身体GT;
     <脚本SRC =JS / app.js>< / SCRIPT>
 < / HTML>

app.js

  VAR IWA = angular.module('inQueueWeb',['ngRoute']);   iwa.config(['$ routeProvider','$ locationProvider',函数($ routeProvider,$ locationProvider){
$ routeProvider
   。当(/,{templateUrl:意见/ home.html做为'})
   。当(/休息,{templateUrl:意见/ about.html'});
$ locationProvider.html5Mode(真);
}]);

和我也尝试了这些

  $ locationProvider.html5Mode({启用:真,requireBase:假});$ locationProvider.html5Mode(真).hash preFIX(!);


解决方案

您是在正确的轨道上。你必须添加 $ locationProvider.html5Mode(真)到应用的的.config()方法,然后添加<基本href =/> 到您的HTML头。我想你已经做到了这一点。

什么错误是这样的:以 $ locationProvider.html5Mode(真),一个hashbang( /#我的页面 )网址是没有必要的。一个URL,简直是 /我的页面,没有散。您需要更新的链接在你的的意见/模板/ header.html中,以适应这一点。

我已经更新:


  • app.js 添加 $ locationProvider.html5Mode(真)

  • index.html的添加<基本href =/>
  • 的意见/模板/ header.html中来调整锚链接。

我希望这是有帮助的!您可以下载档案与文件调整这里

In my angular page i'm using a $locationProvider. But it's not working in my web page. I got error in my console like this, $location in HTML5 mode requires a tag to be present!. so i add a <base href="/" />, which also not working in my angular page. Here i mentioned my directory structure and my code.

Here my sample Project link too. Help me out to resolve this problem.

my directory structure

 iwa (root directory)
  > js
      > lib
         -> angular.js
         -> angular.route.js
      -> app.js
  > views
      -> home.html
      -> about.html
 -> index.html

index.html

 <!DOCTYPE html>
 <html ng-app="inQueueWeb">
 <head lang="en">
     <meta charset="UTF-8">
     <script src="js/lib/angular.js"></script>
     <script src="js/lib/angular-route.js"></script>
  </head>
 <body>
     <div ng-view></div>
 </body>
     <script src="js/app.js"></script>
 </html>

app.js

  var iwa = angular.module('inQueueWeb', ['ngRoute']);

   iwa.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {
$routeProvider
   .when("/", {templateUrl: 'views/home.html'})
   .when("/rest", {templateUrl: 'views/about.html'});
$locationProvider.html5Mode(true);
}]);

And also i tried these

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

$locationProvider.html5Mode(true).hashPrefix('!');

解决方案

You're on the right track. You've got to add $locationProvider.html5Mode(true) to your app's .config() method, and then add a <base href="/"> to your HTML head. I think you've done this already.

What's going wrong is this: with $locationProvider.html5Mode(true), a hashbang (/#mypage) URL isn't necessary. A URL would simply be /mypage, without the hash. You need to update the links in your views/templates/header.html to suit this.

I've updated:

  • Your app.js to add $locationProvider.html5Mode(true).
  • Your index.html to add <base href="/">, and
  • Your views/templates/header.html to adjust the anchor links.

I hope this was helpful! You can download an archive with the adjusted files here.

这篇关于$ locationProvider是不是在我的角度网页工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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