这个问题通常可以通过将值分配给作用域对象来解决。例如,如果您有一个控制器,您可以将它的值分配给 $scope 对象:
app.controller('MyController', function($scope) {
$scope.myValue = 'Hello World';
});
现在,您可以在控制器内部和外部访问 $scope.myValue。如果您在控制器外部访问它,在前面加上 $scope 限定符:
app.controller('MyController', function($scope) {
$scope.myValue = 'Hello World';
});
// ...
angular.element(document).ready(function() {
var controllerElement = document.getElementById('my-controller');
var controllerScope = angular.element(controllerElement).scope();
console.log(controllerScope.myValue); // "Hello World"
});