$Location.path of Angular changed?

Asked

Viewed 305 times

1

Good morning, everyone!

I am trying to make a Function with $Location.path('/main'); and when the function, the following message appears on the console:

Typeerror: Cannot read Property 'path' of Undefined

Why is that?

.controller('loginCtrl', ['$scope', '$stateParams', function ($scope, $stateParams, $http, $location) {

    $scope.fazerLogin = function(usuario){
        /*$http.post("", usuario).success(function(data){

        });*/
    $location.path('/main');
}


}])

1 answer

1


Your first line with the declaration of controller is incomplete. The correct would be:

...
.controller('loginCtrl', ['$scope', '$stateParams', '$http', '$location', function ($scope, $stateParams, $http, $location) {
...

Now a point of improvement. If you use Angular Styleguide (which is a very interesting material that greatly increases the written code), you should declare the controller as follows:

...
.controller('LoginCtrl', LoginCtrl);

LoginCtrl.$inject = ['$scope', '$stateParams', '$http', '$location'];

function LoginCtrl($scope, $stateParams, $http, $location) {
...
  • Blz, it worked. But why do I have to put ['$http', '$Location', Function... tb? Recent change?

  • It is not change, you have to declare all the properties you will use, otherwise it is without reference, and so gave undefined

  • But in another application I have, I didn’t do it that way and it works!!

  • 1

    @Gustavosevero you probably did not declare any, it works. This way of declaring as string serves for when you will minify the file. Here is an explanation in the documentation Dependency Injection. If you want you can open another question asking the staff to explain in detail

Browser other questions tagged

You are not signed in. Login or sign up in order to post.