1

I'm trying to do inline editing in a form with an Angular directive, unfortunately I ran into two issues and I can't get around them, so a second opinion will be very much appreciated.

here is the fiddle: http://jsfiddle.net/jorgecas99/bc65N/

Question 1: As you can see I added a section that is suppose to listen to key strokes (in this case the esc key) and exit the edit mode, unfortunately it is not working. I tried listening for key 13 which is 'enter' and that worked ok, so I don't understand.

Question 2: I will like to change the second field to a dropdown when you click to edit it without having to create a new directive, is that even possible? I certainly care about number of lines of code so if this can be achieve with one directive, then that would be my preferred option.

Thank you!

1 Answer 1

4

for first question, you can see a revised version of your fiddle which incorporate the technique described in http://css-tricks.com/snippets/javascript/saving-contenteditable-content-changes-as-json-with-ajax/ here http://jsfiddle.net/bonamico/cAHz7/

var myApp = angular.module('myApp', []);

please note that var myApp = was missing, and so the following declaration did not execute

myApp.directive('contenteditable', function() {
return {
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
        // view -> model
        elm.bind('blur', function() {
            scope.$apply(function() {
                ctrl.$setViewValue(elm.html());
            });
        });

        // model -> view
        ctrl.render = function(value) {
            elm.html(value);
        };

        // load init value from DOM
        ctrl.$setViewValue(elm.html());

        elm.bind('keydown', function(event) {
            console.log("keydown " + event.which);
            var esc = event.which == 27,
                el = event.target;

            if (esc) {
                    console.log("esc");
                    ctrl.$setViewValue(elm.html());
                    el.blur();
                    event.preventDefault();                        
                }

        });

    }
};

});

See also http://api.jquery.com/keydown/

For the second question, I would suggest that minimizing numer of lines of code is not normally a main concern, while making code modular and maintainable is. So it would be definitely better to create two directives, and possibly a common javascript function for the commonm parts between the two, if any.

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

1 Comment

ctrl.$setViewValue(elm.html()); in the if (esc) block is not needed because you're triggering blur and are already always doing that in the blur handler

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.