28

I am having a Angular scope variable streetName.

<script type="text/javascript">
 angular.module('addApp').controller('add', ['$scope',function($scope) {
 $scope.streetName = "Bonita Ln";
 }]);
</script>

How can I access streetName in a javascript defined under this controller (add) scope. Please help.

<div ng-app="addApp" ng-controller="add">
StreetName: {{streetName}}
<script type="text/javascript">
//here i need to access the value of streetName...
</script>
</div>
S Panfilov
17.7k19 gold badges79 silver badges103 bronze badges
asked Jul 31, 2013 at 2:29

5 Answers 5

38

This way is long but it works:

 angular.element(document.querySelector('[ng-controller="add"]')).scope().streetName

More readable:

 var dom_el = document.querySelector('[ng-controller="add"]');
 var ng_el = angular.element(dom_el);
 var ng_el_scope = ng_el.scope();
 var street_name = ng_el_scope.streetName;

And it's much shorter if you're using jQuery:

 var street_name = $('[ng-controller="add"]').scope().streetName;

Link to jsfiddle demo

Matt Montag
7,6198 gold badges44 silver badges47 bronze badges
answered Jul 31, 2013 at 5:46
Sign up to request clarification or add additional context in comments.

9 Comments

But this is returning null. It couldnt read the controller 'add'.
@JPN Seems like the element is not ready yet. Try to put my code in window.onload handler
Its working :) but sometimes it gives null. Looking at what I am missing.. thanks a ton..!!
@JPN in case of null you can add some little setInterval that will check for element/scope until it is completely ready. Good luck!
@chrmiv I found out the issue... I didnt include the ng-controller in the div tag which wraps around the javascript tag.. now its perfectly working. thank you so much..
|
13

You may pass your scope vat to common js var with $window service.

Like this:

angular.module('addApp', [])
.controller('add', ['$window', function ($window) {
 ...
 $window.streetName = $scope.streetName; 
 ...
}

and attach your js after that in comon js code like that:

<script type="text/javascript">
 document.write("\<script src='...' type='text/javascript'>\<\/script>");
</script>

But keep in mind that it's workaround, not best solution

answered Jul 31, 2013 at 6:56

2 Comments

This is perfect! Just in case it's not clear, you can access values assigned to the angular $window object with the javascript window object.
@Sergey Panfilov : but how can i access that value in myscript value.?
2

I faced a similar problem. I found a work around. It worked well for me but not sure if it is the right approach.

Create a hidden input field in the html and assign it your value from angular. Access this value in javascript. Make sure you create it before your script tag so that it will be initialized when you reach your script tag.

Something like this:

<div ng-app="addApp" ng-controller="add">
 StreetName: {{streetName}}
 <input type="hidden" id="dummyStreetName" value={{streetName}}>
 <script type="text/javascript">
 console.log("I got the street name "+$("#dummyStreetName").val());
 </script>
</div>
answered Oct 25, 2017 at 12:18

1 Comment

Not working, I got "I got the street name {{name}}" in a console
0

Simple, non-complicated, jQuery solution:

HTML

<div id="data" data-street="{{streetName}}"></div>

Javascript

$(document).ready(function() {
 var streetName = $('#data').data('street');
}); 

The $(document).ready... is important to give angular time to set the variables.

answered Nov 29, 2018 at 21:58

Comments

0

In Angular 11 / typescript 4.3.2 I was able to do this by adding this to my ngOnInit:

window.localStorage.setItem("roundChartEdges","false")

then could access the variable in the javascript chartsjs draw function I was overriding:

Chart['elements'].Rectangle.prototype.draw = function () {
 console.log(window.localStorage.getItem("roundChartEdges"))
 ...
}

Not a bad solution if you aren't storing sensitive data.

I needed to programmatically tell chartsjs when to round the bar chart edges and when not to, using variables defined in my ngOnInit function.

answered Jul 8, 2021 at 21:41

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.