Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was looking for a inline editing solution and I found a plunker that seemed promising, but it didn't work for me out of the box. After some tinkering with the code I got it working. Kudos to the person who made the initial effort to code this piece.</p> <p>The example is available here <a href="http://plnkr.co/edit/EsW7mV?p=preview">http://plnkr.co/edit/EsW7mV?p=preview</a></p> <p>Here goes the code:</p> <pre><code>app.controller('MainCtrl', function($scope) { $scope.updateTodo = function(indx) { console.log(indx); }; $scope.cancelEdit = function(value) { console.log('Canceled editing', value); }; $scope.todos = [ {id:123, title: 'Lord of the things'}, {id:321, title: 'Hoovering heights'}, {id:231, title: 'Watership brown'} ]; }); // On esc event app.directive('onEsc', function() { return function(scope, elm, attr) { elm.bind('keydown', function(e) { if (e.keyCode === 27) { scope.$apply(attr.onEsc); } }); }; }); // On enter event app.directive('onEnter', function() { return function(scope, elm, attr) { elm.bind('keypress', function(e) { if (e.keyCode === 13) { scope.$apply(attr.onEnter); } }); }; }); // Inline edit directive app.directive('inlineEdit', function($timeout) { return { scope: { model: '=inlineEdit', handleSave: '&amp;onSave', handleCancel: '&amp;onCancel' }, link: function(scope, elm, attr) { var previousValue; scope.edit = function() { scope.editMode = true; previousValue = scope.model; $timeout(function() { elm.find('input')[0].focus(); }, 0, false); }; scope.save = function() { scope.editMode = false; scope.handleSave({value: scope.model}); }; scope.cancel = function() { scope.editMode = false; scope.model = previousValue; scope.handleCancel({value: scope.model}); }; }, templateUrl: 'inline-edit.html' }; }); </code></pre> <p>Directive template:</p> <pre><code>&lt;div&gt; &lt;input type="text" on-enter="save()" on-esc="cancel()" ng-model="model" ng-show="editMode"&gt; &lt;button ng-click="cancel()" ng-show="editMode"&gt;cancel&lt;/button&gt; &lt;button ng-click="save()" ng-show="editMode"&gt;save&lt;/button&gt; &lt;span ng-mouseenter="showEdit = true" ng-mouseleave="showEdit = false"&gt; &lt;span ng-hide="editMode" ng-click="edit()"&gt;{{model}}&lt;/span&gt; &lt;a ng-show="showEdit" ng-click="edit()"&gt;edit&lt;/a&gt; &lt;/span&gt; &lt;/div&gt; </code></pre> <p>To use it just add water:</p> <pre><code>&lt;div ng-repeat="todo in todos" inline-edit="todo.title" on-save="updateTodo($index)" on-cancel="cancelEdit(todo.title)"&gt;&lt;/div&gt; </code></pre> <p>UPDATE:</p> <p>Another option is to use the readymade Xeditable for AngularJS: </p> <p><a href="http://vitalets.github.io/angular-xeditable/">http://vitalets.github.io/angular-xeditable/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload