Deekor Deekor - 3 months ago 29
AngularJS Question

Angular controller in coffeescript

I'm trying to define an angular controller in my CoffeeScript file.

angular.module("myApp").controller('DashboardReportsController', ['$http', ($http) ->
dctrl = @
dctrl.setupDashboard = (id)->
dctrl.business_id = id
setBreakdownChart()
return null

dctrl.setBreakdownChart = ()->
$http.get("/businesses/" + dctrl.business_id + "/dashboard/breakdown/").success( (data) ->
exrevctx = document.getElementById("expenseRevenueChart")
new Chart(exrevctx, data)
)
return null

return this # Coffeescript auto returns the last line. Angular controllers are constructors so we must return `this`
])


I'm calling
ng-init
here:

<div class="row dashboard-grid" ng-controller="DashboardReportsController as dctrl" ng-init="dctrl.setupDashboard('<%= @business.slug %>')" >


I'm getting the error:

ReferenceError: setBreakdownChart is not defined


Between the error and breakpoints, I know
setupDashbaord
is being called but why is
setBreakdownChart
not defined?

Answer

You forgot to reference the controller. Change it to

dctrl.setBreakdownChart()

rather than

setBreakdownChart()
Comments