Access angular $scope without element

Question!

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);
}


Answers

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.



No you aren't creating an instance by doing that. Creating an instance requires use of the new keyword (or using reflection but thats out of the scope of the question). Somewhere inside db.rawQuery they are creating an instance of a class that implements Cursor. Implementing an interface requires that you have all of the methods it declares implemented (go figure), meaning you can treat that object as though it were an instance of a "Cursor class", and return it to someone expecting a Cursor. They aren't ever directly creating an instance of that interface.



that would change the height of your box and align it center

    .trending-button1{  
    border-radius: 25px;
    border: 3px solid #fff;
    padding: 20px;
    width: 200px;
    max-height: 20px;
    margin:0 auto;
    }
By : kdskii


This video can help you solving your question :)
By: admin