0

My code is as follows:

<!doctype html>
<html ng-app="flyerGen">
<head>
<script src="http://code.angularjs.org/1.0.5/angular.min.js"></script>
<script>
angular.module('flyerGen', []).directive('contenteditable', function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            // view -> model
            elm.bind('keyup', function() {
                scope.$apply(function() {
                    ctrl.$setViewValue(elm.html());
                });
            });

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

            // load init value from DOM
            ctrl.$setViewValue(elm.html());
        }
    };
});
function FlyerCtrl($scope) {
    $scope.Flyer = { bgColor : '231,233,230', title : 'WIE WAREN WIR HEUTE?', description: 'Bitte scannen Sie den QR-Code und geben Sie uns Feedback' }
}

</script>
</head>
<Body>
<div contentEditable ng-model="Flyer.title">{{ Flyer.title }}</div>
Test: {{ Flyer.title }}
</div>
</Body>
</html>

When loading the page I see the following error in my console: Error: No controller: ngModel. I also tried to set "FlyerCtrl", also "Flyer" instead of "ngModel", but nothing works.

Where is the mistake?

1 Answer 1

3

A few items:

  • HTML should have contenteditable not contentEditable
  • do not load the init value from the DOM -- you already have it in your model, so remove this from your HTML: <div...>{{ Flyer.title }}</div> and don't call $setViewValue initially.

Plunker.

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

3 Comments

Now it works (test output does change), but the error is still appearing every time I load the page. Can you solve it?
@Barth, I'm not sure why you are getting that error. Does the Plunker I provided show that error on your system? (I don't see that error on my system when I run that Plunker. I'm using Chrome).
@Barth, I don't download code from the Internet and run it. If you can provide a plunker or jsfiddle showing the problem, I'll look at that.

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.