1

I am using Angular to get output from controller and displaying with ng-bind. but I have a completely separate javascript which would need to use a value returned from angular.

In the code below the TestAppCtrl is called and the output shows correctly in the DOM but when I try to show in the alert box in the second script, it shows as undefined. I have tried multiple things even in my second script getting by element and getting inner html, etc but its always undefined.

<html ng-app="testApp">
<head><script src="lib/angular.min.js"></script> </head>
<body>

    <div ng-controller="TestAppCtrl">
       <div id="data"><span ng-bind="data" ></span></div>
     </div>

  <script>

     var testApp = angular.module('testApp', []);

       testApp.controller('TestAppCtrl', function ($scope) {
       $scope.data = [1, 2, 4, 5];
       });

    </script>



    <script>
    window.alert(data);
    </script>


</body>
</html>
1
  • data from controller is really this simple? No HTTP service involved? Commented Nov 18, 2014 at 22:52

2 Answers 2

3

Please see below for working demo.

var app = angular.module('app', []);

app.controller('TestAppCtrl', function($scope) {
  $scope.data = [1, 2, 4, 5];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="app">
  <div ng-controller="TestAppCtrl" id="TestAppCtrl">
    {{data | json}}
  </div>

  <script>
    angular.element(document).ready(function() {

      var controllerElement = document.querySelector('#TestAppCtrl');
      var controllerScope = angular.element(controllerElement).scope();
      alert(controllerScope.data);
    });
  </script>
</body>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot! Do you know any pages where I can read up on this way of accessing angular ?
@EduardoDennis unfortunately not :(
no worries, just wondering since I hadn't seen the angular.element() yet. I am just starting with angular though so maybe later. Thanks nonetheless for your help :D
0
<html ng-app="testApp">
    <body>

        <div ng-controller="TestAppCtrl">
            <div id="data"><span ng-bind="data" ></span></div>
        </div>

<script>
    var stuff;
    var testApp = angular.module('testApp', []);

    testApp.controller('TestAppCtrl', function ($scope) {
        $scope.data = [1, 2, 4, 5];
        stuff = $scope.data; // try this?
    });


</script>



<script>
    window.alert(stuff);
</script>

EDIT

Okay so maybe initializing stuff inside the controller would help.

1 Comment

it returns undefined, I imagine because the controller is a async the second script gets executed before the stuff variable is populated. I f I hardcode a value into the variable it will show.

Your Answer

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.