0

I have little problem.

I want to create ng-click with redirect, but before redirect I need to post some data.

My html code :

<a href="/test" ng-click="updateData($event)">Go</a>

My angular updateData function :

$scope.updateData = function($event){

   $event.preventDefault();

   $http.post(someAddress, $scope.data)
      .success(function(){
          // on success redirect
      });
   };

I found somethings like this scope.$eval(clickAction); but I don;t know how I should use it.

I can't use only redirect without JS because I have 2 different forms.

3 Answers 3

1
<a href="/test" ng-click="updateData($event, this.href)">Go</a>


$scope.updateData = function($event, loc){
   $event.preventDefault();

   $http.post(someAddress, $scope.data)
      .success(function(){
          // on success redirect
          location.href = loc;
      });
   };
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you guys. It slove my problem.
1

You should redirect from JS function rather than relying on href, no need of $event.preventDefault(); in this case.

Markup

<a href="" ng-click="updateData()">Go</a>

Code

 $scope.updateData = function(){
    $http.post(someAddress, $scope.data)
    .success(function(){
      // on success redirect
      $location.path('/test'); //to redirect from here
    });
 };

2 Comments

Native JS is always faster than any frameworks helper functions.
i don't think so..rather than doing it..I will follow more angular way of coding..
1

For full redirects, try the following:

location.assign("http://someuri.com");

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.