Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

angularjs - Is it possible to get the current $location in a directive in Angular JS?

Something along the lines of: link: function($scope, element, attrs, $location) {

Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  1. $route Docs
  2. $location Docs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...