angularJS1 Error: [$location:nobase]

本文探讨了AngularJS中由于缺少<base href=/>而导致的问题,并提供了解决方案,即通过设置$locationProvider.html5Mode来避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.错误原因:html中缺少了<base href="/">
2.如果html中没写,请加上

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

script代码如下:

<script>
    // absUrl="http://168.33.222.69:8020/angularjs_demo/a.html#name=xie&age=25&gender=nv";
        var app=angular.module("myapp",[]);
        app.config(['$locationProvider', function($locationProvider) { 

         // $locationProvider.html5Mode(true);  使用这个时,html中加入<base href="/">

         //也可以忽略次检查
         $locationProvider.html5Mode({
          enabled: true,
          requireBase: false
        });
        }]);
        app.controller("myctrl",function($scope,$location){
            //a.html?name=xie&age=25&gender=nv
            console.log('absUrl=='+$location.absUrl());
            console.log('host=='+$location.host());
            console.log('port=='+$location.port());
            console.log('protocol=='+$location.protocol());
            console.log('url=='+$location.url());
            console.log('path=='+$location.path());
            console.log('search=='+$location.search().name);

// 控制台打印结果
//[Web浏览器] "absUrl==http://168.33.222.69:8020/angularjs_demo/a.html#name=xie&
// age=25&gender=nv"    
//[Web浏览器] "host==168.33.222.69"    
//[Web浏览器] "port==8020" 
//[Web浏览器] "protocol==http" 
//[Web浏览器] "url==/angularjs_demo/a.html#name=xie&age=25&gender=nv"  
//[Web浏览器] "path==/angularjs_demo/a.html"   
//[Web浏览器] "search==undefined"     这个是什么问题??







        });





    </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值