Batman Batman - 1 year ago 104
AngularJS Question

How to Pass the Angular Variable to Another Angular Google Chart Function(Angular Directive)

Can someone help me in approach for passing Angular Variable to Google Chart Function which is present in the same Java Script with Angular Directive

And Also I wanted to load Google Charts to load only after passing the values.I just read some question they mentioned something like Promises How can i use here.

Java Script

In Below Code I wanted to pass Angular Variables openCount,closedCount to Google Chart Function like with Angular JS

Demo For Google Chart with Angular JS

In the above Demo I am hard coding the values

"rows": [
{c: [
{v: "Defect Opened"},

{c: [
{v: "Defects Closed"},
{v: 2}


Now instead of hard coding values I wanted to pass the values from below controller. How can I achieve this[What modification required for this]

var app = angular.module('defectApp', ["ngTable"]);
app.controller('defectController', [
function($scope, $http) {
$http({method: 'POST', url: '/angular/defect/defect.php'}).success(function(response) {
$ = response;


var openCount = $filter('filter')(, {status: 'Open'}).length,
closedCount = $filter('filter')(, {status: 'Closed'}).length,
assignedCount = $filter('filter')(, {status: 'Assigned'}).length;


Answer Source

One option would be to introduce a service and move the method for getting data from defectController into service:

.service('defectService', function ($http, $filter) {
    return {
        getInfo: function () {
            return $http({ method: 'POST', url: '/angular/defect/defect.php' })
                .then(function (response) {
                    return {
                        "openCount": $filter('filter')(, { status: "Open" }).length,
                        "closedCount": $filter('filter')(, { status: 'Closed' }).length,
                        "assignedCount": $filter('filter')(, { status: 'Assigned' }).length

Then inject defectService service info chart contoller. Modify chart controller to draw a chart once the data is retrieved:

.controller("chartCtrl", function ($scope, defectService) {

        .then(function (info) {

    $scope.drawChart = function (info) {
        //code is omitted for clarity..



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download