Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access $scope from html?

Tags:

angularjs

Because of stupid third-party reasons, I need to access $scope from the html.

This is what I'm attempting:

<html ng-app>
    <!-- head goes here-->
    <body>
        <!--Body goes here-->
        <script type="text/javascript">
            console.log($scope);
        </script>
    </body>
</html>
like image 660
Himmators Avatar asked Oct 21 '13 10:10

Himmators


People also ask

How do you access $scope in console?

To examine the scope in the debugger: Right click on the element of interest in your browser and select 'inspect element'. You should see the browser debugger with the element you clicked on highlighted. The debugger allows you to access the currently selected element in the console as $0 variable.

How do I view angular variables in browser console?

You could use the following in the console: angular. element(document. querySelector('[ng-controller=hw]')).

Can parent controller access the methods of child controller or vice versa?

39) Do you think that parent controller can access the methods of child controller or vice versa? No, the parent controller cannot access the methods of child controller, but the child controller can access the methods of the parent controller.

What directive is used to specify a controller in an HTML element?

The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element.


1 Answers

Because angular is exposed globally, you can use:

var scope = angular.element().scope()

For example if you have this in your markup

<div ng-controller="someCtrl" id="someId">{{test}}</div>

You can access the isolated scope of the controller someCtrl like this:

var scope = angular.element($("#someId")).scope()
scope.test = "Hello, world!";

(you might want to $apply the scope as well, see here)

like image 160
subZero Avatar answered Sep 28 '22 10:09

subZero