Every answer I have found involves using an element and accessing $scope from there.
I want to access scope for a given controller, preferably by controller name, in javascript outside the controller definition. Is this even possible?
For clarity, I'm trying to update a variable being watched on $scope within the controller. I want to have one standalone function that I can pass some parameters into that can perform this function for a multitude of controllers in my application.
If I'm going about this completely the wrong way please correct me. Just started getting in to angular recently.
Something like:
var myapp = angular.module("myapp", []);
myapp.controller("myController1", function($scope, $http){
$scope.specialVar = "some value";
});
myapp.controller("myController2", function($scope, $http){
$scope.specialVar = "some other value";
});
var myFunction = function(controller, val){
//set the value of specialVar for the passed controller to the passed value
myapp.AccessController(controller).$scope('specialVar', val);
}
$scopeoutside a controller is really an anti-pattern in AngularJS. In saying that, if you need to have some shared method that updated$scope, you could have a service method like:service.updateScope = function(scope) { ... }, and then invoke it in your controllerservice.updateScope($scope). Again, I'd recommend against this.