2016-12-15 41 views
0

我需要使用angular.js如何使用Angular.js

任何一个可以帮助我在此得到基本URL获得基本URL?

我尝试了不同的解决方案,但我没有得到正确的URL。

+1

你到目前为止尝试过什么?你是否在控制器中注入'$ location'?试过'$ location。$$ absUrl'? –

+3

[如何获取Angular JS中的URL段?](http://stackoverflow.com/questions/30871479/how-to-get-url-segment-in-angular-js) – GilZ

回答

0

如果你想在使用JavaScript的浏览器基本URL,你可以使用:

window.location.origin

或获得所有部件的对象使用:

window.location

希望这有助于:)

+0

感谢您的信息。 –

0

获取网址的角度方式是使用$ location服务。你需要在任何需要它注入它(控制器,其他服务等)

angular 
    .module('app') 
    .controller('controller', mainController); 

/** @ngInject **/ 
function mainController($location){ 
    console.log($location) //prints all $location properties 
    console.log($location.$$host) // prints 'stackoverflow.com' 
    console.log($location.$$absUrl) // prints 'http://stackoverflow.com/exampleurl' 
} 

你可以做到这一点的无棱角的方式,利用全球window.location的对象:

console.log(location.href) // prints 'http://stackoverflow.com/exampleurl' 

希望它有帮助