1

"<span contenteditable>{{ line.col2 }}</span>"

Hello,

This code is good at initialisation but if I edit the span, no bing is send and my array model never updated...

So, I have tried this :

<span contenteditable ng-model="line.col2" ng-blur="line.col2=element.text()"></span>

But "this.innerHTML" does not exist.

What can I do ?

Thank at all ;-)

0

3 Answers 3

4

you can remove the ng-blur and you will have to add this directive:

<span contenteditable ng-model="myModel"></span>

Here is the directive taken from the documentation:

.directive('contenteditable', function() {
  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(ngModel.$viewValue || '');
      };

      // Listen for change events to enable binding
      element.on('blur keyup change', function() {
        scope.$apply(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);
      }
    }
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank very much ! I didn't know this possibility and it work perfectly. After some editions (see bellow)
You should mention that this directive was taken from angular's documentation. Strengthens your answer ;) Its around line 16327 in angular >= 1.2.6
2

I only will point you to possible solution, then you need to parse/clean HTML better.

<span contenteditable data-ng-blur="bar = $event.target.innerHTML">
    {{bar}}
</span>

// upd.

Angular events such as click, blur, focus, ... - fired with scope context, e.g. this will be current scope. Use $event, be happy.

1 Comment

Thanks very much ! ;-) Now i know how to access to my target. But i have another problem : "the <br> and <div> tags are visibles (parsing fail)" --> I check the directive solution to see if it's better
0

Solution with Mirrage and gab help :

<span contenteditable="true" ng-model="ligne.col2">{{ ligne.col2 }}</span>


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();
    }
  };
});

Thank at all ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.