Micchaleq
Micchaleq

Reputation: 433

ng-click - after click do function and redirect

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.

Upvotes: 0

Views: 1269

Answers (3)

Michael Kang
Michael Kang

Reputation: 52867

For full redirects, try the following:

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

Upvotes: 1

vinayakj
vinayakj

Reputation: 5681

<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;
      });
   };

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136184

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
    });
 };

Upvotes: 1

Related Questions