How to get 2-way binding to work with a span contenteditable="true"
instead of an input?
See Plunker
How to get 2-way binding to work with a span contenteditable="true"
instead of an input?
See Plunker
Take a look at this
Hmtl
<body ng-controller="MyCtrl">
<span contenteditable="true" ng-model="person.name">{{ person.name }}</span>
<pre ng-bind="person.name"></pre>
</body>
script
var app = angular.module('myApp', []);
app.directive('contenteditable', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
// view -> model
element.bind('blur', function() {
scope.$apply(function() {
ctrl.$setViewValue(element.html());
});
});
// model -> view
ctrl.$render = function() {
element.html(ctrl.$viewValue);
};
// load init value from DOM
ctrl.$render();
}
};
});
app.controller('MyCtrl', function($scope) {
$scope.person = {name: 'David'};
});