从同一控制器 Angularjs 中的另一种方法访问$scope变量

Accessing $scope variable from another method in the same controller Angularjs

本文关键字:访问 方法 scope 变量 另一种 控制器 Angularjs      更新时间:2023-09-26

如何在 Angularjs 中从同一控制器中的另一个方法调用/访问$scope.mydata

我的场景是

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

该函数中访问$scope

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

functionA 中访问它一样functionB访问$scope.data。它会起作用。对于这两个函数,$scope变量位于相同的词法范围内。