Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(directives): Add support for contenteditable with ng-model
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Silva authored and mhevery committed Jan 22, 2014
1 parent aeb5538 commit 715d3d1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/directive/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class NgDirectiveModule extends Module {
value(TextAreaDirective, null);
value(InputSelectDirective, null);
value(OptionValueDirective, null);
value(ContentEditableDirective, null);
value(NgModel, null);
value(NgSwitchDirective, null);
value(NgSwitchWhenDirective, null);
Expand Down
20 changes: 20 additions & 0 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,23 @@ class InputRadioDirective {
});
}
}

/**
* Usage (span could be replaced with any element which supports text content, such as `p`):
*
* <span contenteditable= ng-model="name">
*
* This creates a two way databinding between the expression specified in
* ng-model and the html element in the DOM.  If the ng-model value is
* `null`, it is treated as equivalent to the empty string for rendering
* purposes.
*/
@NgDirective(selector: '[contenteditable][ng-model]')
class ContentEditableDirective extends _InputTextlikeDirective {
ContentEditableDirective(dom.Element inputElement, NgModel ngModel, Scope scope):
super(inputElement, ngModel, scope);

// The implementation is identical to _InputTextlikeDirective but use innerHtml instead of value
get typedValue => (inputElement as dynamic).innerHtml;
set typedValue(String value) => (inputElement as dynamic).innerHtml = (value == null) ? '' : value;
}
27 changes: 26 additions & 1 deletion test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('ng-model', () {
var input = probe.directive(InputTextDirective);
input.processValue();
expect(_.rootScope.model).toEqual('def');

}));

it('should write to input only if value is different', inject(() {
Expand Down Expand Up @@ -438,5 +437,31 @@ describe('ng-model', () {
expect(blueBtn.checked).toBe(false);
}));
});

describe('contenteditable', () {
it('should update content from model', inject(() {
_.compile('<p contenteditable ng-model="model">');
_.rootScope.$digest();

expect((_.rootElement as dom.Element).text).toEqual('');

_.rootScope.$apply('model = "misko"');
expect((_.rootElement as dom.Element).text).toEqual('misko');
}));

it('should update model from the input value', inject(() {
_.compile('<p contenteditable ng-model="model">');
Element element = _.rootElement;

element.innerHtml = 'abc';
_.triggerEvent(element, 'change');
expect(_.rootScope.model).toEqual('abc');

element.innerHtml = 'def';
var input = ngInjector(element).get(ContentEditableDirective);
input.processValue();
expect(_.rootScope.model).toEqual('def');
}));
});

});

0 comments on commit 715d3d1

Please sign in to comment.