Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

how can I call/access $scope.mydata from another method in the same controller in Angularjs?

My scenario is

.controller("myCtryl", function($scope,  $http) {
  $scope.functionA = function(){
     $scope.data = "some data";
  }
  $scope.functionB = function(){
     //access $scope.data here//
  }

}
share|improve this question
3  
yeah just access it there, what is the issue – PSL Jan 25 '15 at 18:38
    
I'm getting empty when I tried to alert() it. – user3569641 Jan 25 '15 at 18:38
    
@user3569641 Have you actually called functionA? – Alexis King Jan 25 '15 at 18:39
    
You must be trying to access $scope.data in the other function before the previous function sets the value on it asyncronously. Currently your question is too unclear for us to provide a definitive answer for your issue, – PSL Jan 25 '15 at 18:40
    
yeah. thank you. @PSL. I was trying to access it before the previous function sets value. – user3569641 Jan 25 '15 at 18:41
up vote 2 down vote accepted

It is fine to access $scope in that function.

.controller("myCtryl", function($scope,  $http) {
  $scope.functionA = function(){
     $scope.data = "some data";
  }
  $scope.functionB = function(){
     $scope.data //this id valid
  }

}
share|improve this answer

You can just access $scope.data in functionB the same way you access it in functionA. It will work. The $scope variable is in the same lexical scope for both functions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.