Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@petebacondarwin petebacondarwin commented Mar 18, 2012

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