AngularJS ng-href does not go to link

I already checked the docs and ng-href doesn't work , but I'm at a dead end.

Does ng-href need a full path? Mine currently looks like <a ng-title="{{title.text}}" ng-id="{{id.num}}" ng-href="/page.php#param:{{id.num}}"><span>go here</span></a> , but when I click on it while it correctly changes the URL in the address bar of the browser (and this is a legitimate URL, I can press "enter" in the address bar, and he will "go" there)), he actually does not go to this page.

What will go wrong? Do I need to combine this with ng-click ? And if so, why?

UPDATE: the link goes to the same page from which it is called, but with a different parameter for a different data display. I think this may have something to do with this ...

+7
angularjs angularjs-ng-href
source share
4 answers

Here is how I decided to solve this problem:

Template:

 <a ng-title="{{title.text}}" ng-id="{{id.num}}" ng-click="go_to_item(id.num)"> <span>{{title.text}}</span> </a> 

JavaScript:

  $scope.go_to_item = function(display) { window.location = '/page.php#id:' + display; location.reload(); }; 

This works in our application. Why Angular-specific $location and $window do not work is a mystery to me. I tried them first, and they did not, so if anyone can explain why, I will let you β€œaccept the answer” to this question !; -)

+1
source share

The easiest way to do this is to add target = "_ self" , in your case this is the solution:

 <a ng-title="{{title.text}}" ng-id="{{id.num}}" ng-href="/page.php#param:{{id.num}}" target="_self"> <span>go here</span> </a> 

No ng-click and no AngularJS function required.

+8
source share

I tried your code and it worked for me:

 <script> $scope.title = {text: "test"}; $scope.id = {num: 123}; </script> <a ng-title="{{title.text}}" ng-id="{{id.num}}" ng-href="/page.php#param:{{id.num}}"><span>go here</span></a> 

will look like this:

 <a ng-title="test" ng-id="123" ng-href="/page.php#param:123" href="/page.php#param:123"><span>go here</span></a> 
0
source share

I had this problem before. For some reason, I was not allowed to have ng-href inside ng-controller .

-5
source share

All Articles