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
3
  • 2
    You need inject $location in your controller. Commented Aug 18, 2017 at 11:20
  • Detailed answer is already provided here in stackoverflow Commented Aug 18, 2017 at 11:33
  • Inject $location in your controller and don't forget to remove it from the success callback function. Commented Aug 18, 2017 at 12:58

3 Answers 3

3

You need to inject $location to your controller,

app.controller('formCtrl',function($scope,$http,$location){    
Sign up to request clarification or add additional context in comments.

4 Comments

Error: $injector:unpr Unknown Provider Unknown provider: $urlProvider <- $url
i do not see anywhere urlProvider
Well dont work, because path is not defined @Sajeetharan
What ? I don't get it
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){ 

3 Comments

@Maxwelt Unknown provider: $urlProvider <- $url
inject '$state' into your controller's dependencies
app.controller('formCtrl',function($scope,$http, $state){
-1

You can redirect using $window in your controller.

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

Hope this helps you

2 Comments

Error: $injector:unpr Unknown Provider Unknown provider: $urlProvider <- $url
$window.location.href should be used only for full page reload.

Your Answer

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