Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
158
rated 0 times [  165] [ 7]  / answers: 1 / hits: 34059  / 11 Years ago, sat, june 22, 2013, 12:00:00

My controller:



$scope.totals = totals;


My template (works as expected for html injection):



{{totals}}


But what I need is to access the variable totals in a script in the template, like so:



<script>
var totals = ????;
// do stuff with totals
</script>


I've tried $scope.totals, $totals, {{totals}}, etc, with no avail. I would appreciate any insight, thanks!



EDIT:



The following is a jsfiddle of my controller and template. Inside of the template I'm wanting to insert a script that uses the $scope.totals variable.



http://jsfiddle.net/38CrC/


More From » html

 Answers
9

OP Answer:



I ended up having to use a directive to find the value I wanted. In the controller that set the value I wanted to use I added a directive so that it looked like this:



'use strict';

angular.module('AdminApp')
.controller('AdminEventsCtrl', function ($scope, collection) {
$scope.totals = collection;
}).directive('foo', function(){
return {
restrict: 'A',
link: function(scope, elem, attrs){
//Use scope.totals here
}
}
});


In my template I had the declaration:



<div foo></div>


This gave me the ability to do what I needed with the variable totals.



Special thanks to @jvandemo for helping me arrive at this answer.


[#77471] Friday, June 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
alejandro questions
Mon, Jul 18, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Thu, Sep 10, 20, 00:00, 4 Years ago
;