Skip to Content

AngularJs and CoffeeScript

Ok, now everybody know I love Java 8. But I also love CoffeeScript when it comes to web development. I think it is much easier to read than Javascript. Maybe its because I also love Ruby…

I also like AngularJs a lot. Despite the fact that it’s a full fledge framework (which I tend to dislike), I really enjoy using it. But I still have a lot to learn…

Now knowing that I love both Coffee and Angular, the first thing I tried, of course, was to use both.

angular.module 'myapp', []

.controller 'DemoController', class
	constructor: (@$location) ->
		@field = 'Field'

	method: ->

	methodWithParam: (param) ->
		"Param: #{param}"

	accessFieldFromMethod: ->
		"Path: #{@$location.absUrl()}"```


<!DOCTYPE html>
<html lang="en" ng-app="myapp">
  <meta charset="UTF-8">
  <script src=""></script>
  <script src=""></script>
<body ng-controller="DemoController as demo">




I think its nice and easy to read. It uses the new “Controller as” syntax in Angular to better isolate the different scopes.

Be careful though: the html sample will work only if your web server converts coffee to js on the fly. Otherwise, you have to do the compilation on client side using coffee-script.js script.

comments powered by Disqus