1

I have Angularjs variable that I want to pass in javascript variable. I found solution here so it didn't work. My code :

<script>
var demo=angular.module('demo', []);
demo.controller('metrics', function($scope, $http) {
$http.get('http://localhost:8080/metrics').then(function(response) 
    {
     $scope.metrics = response.data;            
    });
    });
</script>

<div ng-controller="metrics">
<p>{{metrics}}<p>
</div>
<script>
var e = angular.element(document.querySelector('[ng-controller="metrics"]')); 
var e1 = e.scope().metrics ; 
alert(e1)   
</script>

I got result from {{metrics}} but e1 didn't return something.
Thanks for your help.

1
  • Your issue is timing. $scope.metrics will be populated quite a bit later than you're trying to get it. It's a bad idea to poke around the innards of Angular like this anyway; you should angularify that bit of Javascript there; what exactly are you trying to do?
    – deceze
    Commented Mar 29, 2017 at 12:44

2 Answers 2

1

From your current code, by the time the JS code executes, it will not contain metrics data.

The JS code should be written in the then response of the API call,

If you do that, the JS code will be called after the metrics gets a value.

var demo=angular.module('demo', []);
demo.controller('metrics', function($scope, $http) {
   $http.get('http://localhost:8080/metrics').then(function(response) 
    {
       $scope.metrics = response.data;            
       var e = angular.element(document.querySelector('[ng-controller="metrics"]')); 
       var e1 = e.scope().metrics ; 
       alert(e1);
    });
});
2
  • Of course, this does not actually "pass the value to Javascript"; and since you're doing your work in the callback, there's absolutely no need to do the song and dance with document.querySelector and so on either, just alert(response.data) would do.
    – deceze
    Commented Mar 29, 2017 at 13:16
  • @deceze, the questioner is just trying to pass the result into a javascript variable. I know that the result is direct. But as he is eager to pass to js variable using Javascript, I just solved the issue he is facing. As far as the question is considered, my answer is correct. No need to Downvote it.
    – Sravan
    Commented Mar 29, 2017 at 13:20
1

angular.element() is a function in angular to get a raw DOM

if you want to get a particular <p> then assign id attribute on it

with that raw DOM, you can add attribute, add classes or else (check this link for more info)

but if you want to do something with metrics then just use $scope.metrics after you get it from $http

<div ng-controller="metricsCtrl">
  <p id="metric">{{metrics}}<p>
</div>

<script>
  var demo=angular.module('demo', []);
  demo.controller('metricsCtrl', function($scope, $http) {
  $http.get('http://localhost:8080/metrics').then(function(response) 
      {
       $scope.metrics = response.data;

       // you can do whatever with $scope.metrics here
       console.log($scope.metrics);

      });
  var e = angular.element(document.querySelector('#metric'));
  console.log(e);
  e.addClass('some-class');     // here p will be <p class="some-class" id="metric">{{metrics}}<p>
  });
</script>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.