Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  170] [ 2]  / answers: 1 / hits: 74189  / 11 Years ago, wed, july 31, 2013, 12:00:00

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>

More From » angularjs

 Answers
14

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


[#76638] Monday, July 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maxinec

Total Points: 117
Total Questions: 116
Total Answers: 116

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
maxinec questions
;