1

I am trying to get the content out of an editable div. But i am stuck at implementing the angular template for it.

I am not sure how to map model data from html template to angular controller.

<div ng-repeat="todo in todos">                 
    <div class="todo.id" ng-model={{ ? }} contenteditable="true">
        <pre>{{ todo.text }}</pre>                          
    </div>
    <button type="button" class="btn btn-link"
        ng-click="editTodo(todo.id)">Edit</button>
</div>

I want to pass whatever text i have under

<div class="todo.id"></div> 

Since my div is under ng-repeat, i cannot set ng-model to one scope variable.

What should i have within "ng-model={{ ? }}". Or is there a workaround for such a scenario.

Please guide me on this.

14
  • i dont understand ur scenario... Commented Dec 13, 2014 at 17:30
  • I have added more desc. Hope its clear now. I dont know what to write within 'ng-model' so that i can pass the value within a particular div, to angular controller. Commented Dec 13, 2014 at 18:15
  • still its unclear. u dont tell us what you exactly want? u want new scope? or u want to show ng repeat values in div tag.....? Commented Dec 13, 2014 at 18:29
  • Try ng-model="model[$index]" Commented Dec 13, 2014 at 18:30
  • u want to bind todo-text vale to new model??? Commented Dec 13, 2014 at 18:31

1 Answer 1

4

To make a div content editable in angular, you need to create a contenteditable directive.

There is an example for this in the angular documentation here:

https://docs.angularjs.org/api/ng/type/ngModel.NgModelController#custom-control-example

and a great blog entry from Gabo Esquivel on the topic here: http://gaboesquivel.com/blog/2014/in-place-editing-with-contenteditable-and-angularjs/

As you can see in Gabriel's example, the code for such a directive would be like this (I've added some comments to better understand whats going on):

app.directive("contenteditable", function() {
  return {
    require: "ngModel",
    link: function(scope, element, attrs, ngModel) {

      //read the text typed in the div (syncing model with the view)
      function read() {
        ngModel.$setViewValue(element.html());
      }

      //render the data now in your model into your view
      //$render is invoked when the modelvalue differs from the viewvalue
      //see documentation: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController#
      ngModel.$render = function() {
        element.html(ngModel.$viewValue || "");
      };

      //do this whenever someone starts typing
      element.bind("blur keyup change", function() {
        scope.$apply(read);
      });
    }
  };
});

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that. I am too new too understand all that. I will learn more and try that out :)
I highly recommend that you learn directives, services, and factories. They are the real powerhouses behind Angular. Dan Wahlin has great video tutorials on how to create directives. I highly recommend looking into them.
@user3527354 I got this working now. I guess that implementing a directive for a div type is mandatory to be editable(unlike input tag). Thanks alot.

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.