Skip to content

Instantly share code, notes, and snippets.

@francoishill
Created January 29, 2014 18:19
Show Gist options
  • Select an option

  • Save francoishill/8693785 to your computer and use it in GitHub Desktop.

Select an option

Save francoishill/8693785 to your computer and use it in GitHub Desktop.
# CoffeeScript directive to focus an element when a property changes
# Thanks to: http://stackoverflow.com/questions/14833326/how-to-set-focus-in-angularjs
masterApp.directive "takeFocus", ["$timeout", "$parse", ($timeout, $parse) ->
link: (scope, element, attrs) ->
model = $parse(attrs.takeFocus)
scope.$watch model, (value) ->
if value is true
$timeout ->
element[0].focus()
return
return
element.bind "blur", ->
scope.$apply model.assign(scope, false)
return
return
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment