3

I want to know how to redirect to another page using Angular Js.

I already follow several questions here and don't find any answer with works successfully

This is my code :

var app = angular.module('formExample',[]);
app.controller('formCtrl',function($scope,$http){    
    $scope.insertData=function(){      

      //  if($scope.name =='' && $scope.email == '' && $scope.message = '' && $scope.price =='' && $scope.date == null && $scope.client == ''){return;}
        $http.post("/php/login.php", {
           "email": $scope.email, "password": $scope.password
        }).then(function(response, $location){
                alert("Login Successfully");
                $scope.email = '';
                $scope.password = '';
                $location.path('/clients');

            },function(error){
                alert("Sorry! Data Couldn't be inserted!");
                console.error(error);

            });
        }
    });

I´m getting this error:

TypeError: Cannot read property 'path' of undefined
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • 2
    You need inject `$location` in your controller. – alexmac Aug 18 '17 at 11:20
  • Detailed answer is already provided [here](https://stackoverflow.com/questions/27941876/how-to-redirect-to-another-page-using-angular-js) in stackoverflow – Dev Gosain Aug 18 '17 at 11:33
  • Inject $location in your controller and don't forget to remove it from the success callback function. – Vivz Aug 18 '17 at 12:58

3 Answers3

3

You need to inject $location to your controller,

app.controller('formCtrl',function($scope,$http,$location){    
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You can use a plain JS

window.location.href = '/my-other-page'

Or, if you are using 'ui-router'

$state.reload()

or

$state.go($state.current.name, {}, {reload: true})

don't forget to inject $state into your controller dependencies:

app.controller('formCtrl',function($scope, $http, $state){ 
Maxwellt
  • 126
  • 5
-1

You can redirect using $window in your controller.

$window.location.href = '/index.html';

Hope this helps you