Skip to content

Instantly share code, notes, and snippets.

@pkrnjevic
Forked from anonymous/jsbin.egowew.coffee
Created August 15, 2013 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkrnjevic/6238599 to your computer and use it in GitHub Desktop.
Save pkrnjevic/6238599 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
Author

A sample of how to use a CoffeeScript class to implement an Angular Service, using Service, Factory and Provider styles. See my blog for details.

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