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//
}
}
asked Jan 25, 2015 at 18:37
user3569641
9421 gold badge19 silver badges51 bronze badges
2 Answers 2
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
}
}
answered Jan 25, 2015 at 18:41
Rajdeep Dosanjh
1,1971 gold badge9 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
answered Jan 25, 2015 at 18:38
Alexis King
44.1k17 gold badges144 silver badges218 bronze badges
Comments
lang-js
functionA?$scope.datain 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,