angularjs – 无法获得当前路线. $location.path()返回空.

栏目: 编程语言 · AngularJS · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/27352371/cannot-get-current-route-location-path-returns-empty

我目前在/ addDoc,我想要当前的路线.这是我的控制器:

app.controller('DocRegistrationController',[ '$http', '$scope', '$upload', '$location', function($http, $scope, $upload, $location){
$scope.validation=function(){
   alert($location.path());
}
    }

但是它返回空.我不想硬编码所有路线.我究竟做错了什么?

$location服务响应解析浏览器地址栏中的URL并使URL可用于您的APP.

因为您使用常规URL路径和搜索段,所以必须将$locationProvider html5Mode设置为true.

$locationProvider将使用hashbang作为默认模式.

如果未将html5Mode设置为true,则在尝试获取url路径时将获得空字符串.

然后在设置html5Mode之后使用$location服务获取url路径.

并编写自己的规则来处理路径.

假设您的完整网址如下所示: http://example.com/person/show/321

Main.js

angular.module("MyAPP",[],function($locationProvider){
    $locationProvider.html5Mode(true);
});

function MainController($location){
    var pId = $location.path().split("/")[3]||"Unknown";    //path will be /person/show/321/, and array looks like: ["","person","show","321",""]
    console.log(pId);
}

的index.html

<!DOCTYPE html>
<html lang="en" ng-app="MyAPP">
    <head>
        <meta charset="utf-8">
        <title>Angular test</title>
    </head>
    <body ng-controller="MainController">
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
        <script src="js/main.js"></script>
    </body>
</html>

希望这对你有所帮助.

翻译自:https://stackoverflow.com/questions/27352371/cannot-get-current-route-location-path-returns-empty


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

精通Spring

精通Spring

罗时飞 / 第1版 (2005年4月1日) / 2005-4 / 39.0

本书深入剖析了当前流行的轻量级开发框架Spring技术。本书总共分成3部分。第一部分,重点阐述Spring的架构。这部分内容循序渐进带领开发者进入Spring中。主要在于阐述Spring IoC和Spring AOP。第二部分,重点阐述Spring的使用。这部分内容从简化Java/J2EE的角度出发,从J2EE平台各个技术层面分析、并给出大量的研究实例,对Spring提供的API进行阐述。主要在于......一起来看看 《精通Spring》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具