Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
cg-contenteditable - AngularJS directive to manage contenteditable element (in CoffeeScript)
angular.module('whatever').directive 'cgContenteditable', ->
restrict: 'A'
ngModel: '='
link: (scope, elem, attrs) ->
elem = elem[0]
# 1
elem.setAttribute 'contenteditable', true
# 2
elem.addEventListener 'input', ->
return if scope.ngModel is elem.innerHTML
scope.ngModel = elem.innerHTML
# 3
scope.$watch 'ngModel', ->
return unless scope.ngModel
return if scope.ngModel is elem.innerHTML
elem.innerHTML = scope.ngModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment