Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a generic approach with a base class:</p> <p><a href="http://www.devign.me/angular-dot-js-coffeescript-controller-base-class/" rel="nofollow">http://www.devign.me/angular-dot-js-coffeescript-controller-base-class/</a></p> <p><strong>BaseCtrl.coffee</strong></p> <pre><code># dependency - Function.prototype.bind or underscore/lodash app = angular.module 'someApp' class @BaseCtrl @register: (app, name) -&gt; name ?= @name || @toString().match(/function\s*(.*?)\(/)?[1] app.controller name, @ @inject: (args...) -&gt; @$inject = args constructor: (args...) -&gt; for key, index in @constructor.$inject @[key] = args[index] for key, fn of @constructor.prototype continue unless typeof fn is 'function' continue if key in ['constructor', 'initialize'] or key[0] is '_' @$scope[key] = fn.bind?(@) || _.bind(fn, @) @initialize?() </code></pre> <p><strong>BookFormCtrl.coffee</strong></p> <pre><code>app = angular.module 'someApp' class BookFormCtrl extends BaseCtrl @register app # list of dependencies to be injected # each will be glued to the instance of the controller as a property # e.g. @$scope, @Book @inject '$scope', 'Book' # initialize the controller initialize: -&gt; @$scope.book = title: "Hello" # automatically glued to the scope, with the controller instance as the context/this # so usable as &lt;form ng-submit="submit()"&gt; # methods that start with an underscore are considered as private and won't be glued submit: -&gt; @Book.post(@$scope.book).then =&gt; @$scope.book.title = "" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload