0

I found an excellent fiddle showing an inline edit directive in AngularJS 0.10. Unfortunately, the code doesn't work in the post-1.0 releases. Does anyone have an example of similar functionality in a recent Angular release?

I managed to make some progress by changing model: 'accessor' to model: '&', but when I press enter, the text value is unchanged. The AngularJS documentation about the scope parameter of a directive definition object is completely impenetrable to me.

2
  • Of course, as soon as I post this I find a solution. Commented Jan 14, 2013 at 17:21
  • 1
    here's a good answer about different ways to bind to data in an isolate scope stackoverflow.com/a/13033249/527968 Commented Jan 14, 2013 at 17:47

2 Answers 2

4

If you want a simple bi-directional binding use model: '=' instead of model: '&'

Directive:

app.directive('inlineEdit', function() {
    return {
        restrict: 'E',           
        templateUrl: 'componentTpl.html',
        scope: {
            model: '=' 
        }
    };
});

Template:

<script type="text/ng-template" id="componentTpl.html">  
    <span ng-hide="editMode" ng-click="editMode=true;value=model">{{model}}</span>
    <input type="text" ng-model="value" ng-show="editMode" ng-model-instant ng-enter="editMode=false" ng-change="model=value"/>
</script>

jsFiddler: http://jsfiddle.net/XuhzP/147/

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

1 Comment

Could have sworn I tried = at one point as well. Thanks heaps!
1

You should use '=' binding (bi-directional) and then directly bind to 'model' property of scope.

Here is corrected sample: http://plnkr.co/edit/tjTsNiQ7t0xMWkCEAzwo?p=preview

(its a same solution as @bmleite gave)

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.