Skip to content

Instantly share code, notes, and snippets.

@ludicast
Created March 18, 2012 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludicast/2079970 to your computer and use it in GitHub Desktop.
Save ludicast/2079970 to your computer and use it in GitHub Desktop.
angular.coffee
# in order to do this:
# <div ng:controller="MyController">{{message}}</div>
# I used to be able to
class MyController
constructor:->
@message = "Hello World"
# Now I need:
class MyControllerWithScope
constructor:($scope)->
$scope.message = "Hello World"
# But if you want original way you now can do
adaptForAngular(MyController)
--------------------
adaptForAngular = (clazz, injections...)->
injections.push "$scope"
@[clazz.name] = (args..., scope)->
controller = new clazz()
angular.extend scope, controller
scope.initializeInjections? args...
scope.$scope = scope
@[clazz.name].$inject = injections
@petebacondarwin
Copy link

MyController = ($scope, $otherService)->
  someInternalField = "secret"

  someHiddenHelper = ()->
    #  I can use $scope and $otherService in here
    return $otherService.doStuff($scope.someField)

  $scope.someScopeHelper = ()->
    $scope.fieldToUpdate = someHiddenHelper()

  # I can initialize $scope here
  $scope.someField = "myValue"

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