1

I am trying to build a login functionality using angular; but I get reference error:

ReferenceError: $state is not defined 

I understand $state is not working from inside login function but I am not sure what is the correct procedure to redirect user once login function is called.

My Code:

(function () { 'use strict'; angular .module('app') .controller('authController', authController); authController.$inject = ['$scope','$state']; function authController($scope,$state) { $scope.login = login; $scope.user = {email: '', pass: ''}; }; function login(){ this.dataLoading = true; $state.go("dashboard"); } })(); 
1
  • Just define login() function within authController(). Commented Dec 13, 2015 at 17:33

1 Answer 1

1

That login function should be inside the controller context to get access the $state dependency which has been inject in controller function.

As you wanted to call login function from html, then its should be placed in controller only.

function authController($scope,$state) { $scope.login = login; $scope.user = {email: '', pass: ''}; function login(){ this.dataLoading = true; $state.go("dashboard"); } $scope.login = login; //to call method from html }; 

OR

Still you wanted to place that function outside angular context for some technical reason(Ideally it shouldn't) then you could get access to $state service using angular.injector

angular.injector(['app']).get('$state').go("dashboard"); 
Sign up to request clarification or add additional context in comments.

10 Comments

angular.injector() doesn't require module name??
First option is possible but I was hoping to get it done in login function; rather than bringing it back into main controller; the second option is hacky; maybe my logic is incorrect ?
@ArashKiani from where you are calling login method?
@PankajParkar from my view ng-submit="login()"
@PankajParkar sorry I noticed the modification right after i send the message; Its working fine now! great job thanks :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.