Skip to content

Instantly share code, notes, and snippets.

@corbanbrook
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save corbanbrook/9101705 to your computer and use it in GitHub Desktop.

Select an option

Save corbanbrook/9101705 to your computer and use it in GitHub Desktop.
Lately I've been thinking about better ways of namespacing directives and more specifically
the need to create global directives for one-off pieces of code that are specific to the
context of given controller. Often the only reason I create a directive is because I need
a link function to do some DOM manipulation. Here is my dirty little experiment:
app.directive 'pxLink', ->
link: ($scope, $el, $attrs) -> $scope[$attrs.pxLink]?($el, $attrs)
<div class="thing" px-link="myLinker"></div>
or
<div class="thing" px-link="myLinker" ng-controller="ThingController"></div>
myLinker is a function on the controllers scope which receives the element and attrs:
app.controller 'ThingController', ($scope) ->
$scope.myLinker = (el, attrs) ->
el.css(width: '100px', height: '100px', 'background-color': 'red')
Now simple linking can be done without getting in your way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment