In the declaration of the directive, inject the location service.
app.directive('myDirective', ['$location', function($location) {
return {
link: function(scope, elem, attrs) {
// path() and url() can be used as getters or setters
console.log($location.url());
console.log($location.path());
}
};
}]);
If you are attempting to get the current location, use location.path()
or, alternatively, use the $route
service.
Information on both:
- $route Docs
- $location Docs
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…