Skip to content

Instantly share code, notes, and snippets.

Created August 15, 2013 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/6238592 to your computer and use it in GitHub Desktop.
Save anonymous/6238592 to your computer and use it in GitHub Desktop.
class MyFunc
constructor: ->
@name = "default name"
return "Hello from MyFunc(). this.name = " + @name
$get: ->
@name = "new name"
"Hello from MyFunc.$get(). this.name = " + @name
app = angular.module("app", [])
app.service "myService", MyFunc
app.factory "myFactory", MyFunc
app.provider "myProv", MyFunc
MyCtrl = ($scope, myService, myFactory, myProv) ->
$scope.serviceOutput = "myService = " + myService
$scope.factoryOutput = "myFactory = " + myFactory
$scope.providerOutput = "myProvider = " + myProv
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta name="description" content="Simple Angular service, factory and provider; using CoffeeScript class" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.1/angular.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body ng-controller="MyCtrl">
{{serviceOutput}}
<br/><br/>
{{factoryOutput}}
<br/><br/>
{{providerOutput}}
</body>
</html>
@pkrnjevic
Copy link

This jsbin demonstrates how to use a CoffeeScript class to implement an Angular Service, using Service, Factory and Provider styles. You may have to click "Run with JS" to update Output pane. Details on my blog (http://blog.krnjevic.com)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment