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


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

查看所有标签

猜你喜欢:

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

Coming of Age in Second Life

Coming of Age in Second Life

Tom Boellstorff / Princeton University Press / 2008-04-21 / USD 29.95

The gap between the virtual and the physical, and its effect on the ideas of personhood and relationships, is the most interesting aspect of Boellstorff's analysis... Boellstorff's portrayal of a virt......一起来看看 《Coming of Age in Second Life》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换