0

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);
}
asked Sep 28, 2016 at 20:17
9
  • 2
    use a service to share and store and bind data. Not 100% clear what you are trying to do though...or what higher level problem you are trying to solve Commented Sep 28, 2016 at 20:25
  • @charlietfl added an example Commented Sep 28, 2016 at 20:31
  • so where were you intending on using that function? inside the app? If so you definitely would use a service Commented Sep 28, 2016 at 20:35
  • @charlietfl yes Commented Sep 28, 2016 at 20:36
  • 3
    @RandyHall Accessing $scope outside 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 controller service.updateScope($scope). Again, I'd recommend against this. Commented Sep 28, 2016 at 20:43

1 Answer 1

3

Remember that you can have any number of instances of a given controller coexisting in your application, but only one service; services are effectively singleton. If you're able to assume that there's only ever exactly one instance of a given controller, then it sounds like you're treating that controller as a service, and that you should just use the service instead. If you're not able to assume that there's only ever exactly one instance, then you absolutely need to access through angular.element(domElement).scope(), because otherwise you're going to have a hard time selecting which of the many instances you're talking about.

Remember, you can always use a service and a dictionary/object to map from an arbitrary key into a given scope, if you'd rather not use the element to look up the scope.

answered Sep 28, 2016 at 20:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.