3

am trying to write an inline editing function without using a template as outlined here http://plnkr.co/edit/EsW7mV?p=preview

4
  • It might help to understand what you are trying to achieve by writing your directive without a template? There are a couple of ways of going about it (a template is probably the best way, however) but without knowing what you are trying to achieve, I don't know which approach to show. Aug 27, 2013 at 10:46
  • So what we have to do here?
    – BKM
    Aug 27, 2013 at 11:43
  • Template is the best way, i agree. But when we have dynamic values, template turns out to be a big problem. So what i am looking for is an example of in-line editing that does not use a template. Hope this helps.
    – Priya Bose
    Aug 28, 2013 at 5:46
  • well u have to write the HTML somewhere, and ur options as far as i know r (1) like u did (2) in the directive write html instead the url of the template (3) inside the link func start writing relevat html. i do believe that what u did is the "best practice" Aug 28, 2013 at 7:58

1 Answer 1

6

You can just place the code of the template in the main page.

<li ng-repeat="todo in todos" inline-edit="todo.title" on-save="updateTodo(todo.title)" on-cancel="cancelEdit(todo.title)">
  <div>
  <input type="text" on-enter="save()" on-esc="cancel()" ng-model="model" ng-show="editMode">
  <button ng-click="cancel()" ng-show="editMode">cancel</button>
  <button ng-click="save()" ng-show="editMode">save</button>
  <span ng-mouseenter="showEdit = true" ng-mouseleave="showEdit = false">
    <span ng-hide="editMode" ng-click="edit()">{{model}}</span>
    <a ng-show="showEdit" ng-click="edit()">edit</a>
  </span>
  </div>
</li>

Here there is a fiddle:

http://jsfiddle.net/siliconball/QwDn9/2/

Also temeber to take away the templateUrl: 'inline-edit.html'

If you need the controller scope for any reason place scope: false in the directive. But then you will have to track which option are you editing in any moment (maybe using the id). If that is your situation i suggest to refactor a bit, as you may know, probably is not the best option.

If your situation, i guess it is, is that you want to write it all in one page because you are generating it through some CGI or dynamic content script and you don't want to write the same code in different pages (+scripts interfaces ...), then i suggest also to move the inline-edit="todo.title" and all the directive stuff in the <div> for the sake of orthogonality.

1
  • They all have ng-model="model", I don't understand why it supports multiple editing at the same?
    – Saorikido
    May 26, 2015 at 8:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.