Skip to content Skip to sidebar Skip to footer

Accessing $scope Variable From Another Method In The Same Controller Angularjs

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 =

Solution 1:

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
  }

}

Solution 2:

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.


Post a Comment for "Accessing $scope Variable From Another Method In The Same Controller Angularjs"