3

I am trying to change the href value on click of it.

This is what I have tried.

HTML:

<a href="#Page1">Demo</a>

JS:

    angular.config(function ($routeProvider) {
        $routeProvider
    .when('/Page1', {
            templateUrl: 'main.html'
            controller: 'FirstController'
        })
 .when('/Page2', {
            templateUrl: 'sub.html'
            controller: 'FirstController'
        })
.otherwise('main.html');

How do I change the path of the anchor tag everytime when I clicked on it.

Can anyone please help me out.

1
  • 1
    You should use ng-href to change the value of an href attribute. This will automatically change the href when your controller value changes. Commented Jun 23, 2014 at 12:30

2 Answers 2

5

Use different controllers for each page. You can use a variable for link and set it in the related controller. For example:

<a href="#/{{myLink}}">Demo</a>

or

<a ng-href="#/{{myLink}}">Demo</a>

And in the each related controller:

$scope.myLink="page1";

or

$scope.myLink="page2";

etc.

Or if you insist to use same controller, you can check the location path:

if ($location.path().substring(1) == "page1") {
    $scope.myLink="page2";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Its similar, but ng-href is a slightly better option!
not that useful without explaining or linking to an explanation for why it's a better option... - nevermind, just found it in a comment above and the other solution below.
3

Use ng-href instead - it will automatically update when the bound value changes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.