AngularJS - ng-model fails on contenteditable <span> AngularJS - ng-model fails on contenteditable <span> angularjs angularjs

AngularJS - ng-model fails on contenteditable <span>


ng-bind! Use ng-bind for one-way binding in 'span'.

Please refer here for an example: https://docs.angularjs.org/api/ng/directive/ngBind

So your line would be:<span contenteditable="true" ng-bind="chunk.value"></span>

Hope this help


To make ng-model work with contenteditable <span> elements, use a custom directive:

app.directive('contenteditable', ['$sce', function($sce) {    return {      restrict: 'A', // only activate on element attribute      require: '?ngModel', // get a hold of NgModelController      link: function(scope, element, attrs, ngModel) {        if (!ngModel) return; // do nothing if no ng-model        // Specify how UI should be updated        ngModel.$render = function() {          element.html($sce.getTrustedHtml(ngModel.$viewValue || ''));        };        // Listen for change events to enable binding        element.on('blur keyup change', function() {          scope.$evalAsync(read);        });        read(); // initialize        // Write data to the model        function read() {          var html = element.html();          // When we clear the content editable the browser leaves a <br> behind          // If strip-br attribute is provided then we strip this out          if (attrs.stripBr && html === '<br>') {            html = '';          }          ngModel.$setViewValue(html);        }      }    };}]);

Usage:

<span contenteditable ng-model="userContent">Change me!</span><p>{{userContent}}</p>

For more infomation, see


The DEMO