1

I got a TypeError: Cannot set property '$render' of null because I didn't set the model in some of the components. So, how could I do this:

.directive("contenteditable", function () {
        return {
            restrict: "A",
            require: "?ngModel",

and create a <div contenteditable class="title-module-name" required></div> without a ng-model? Maybe I need to check before:

ngModel.$render = function () {
   element.html(ngModel.$viewValue || "");
};

but I don't know how to do this!

Thank you!

My entire directive:

.directive("contenteditable", function () {
    return {
        restrict: "A",
        require: "?ngModel",
        link: function (scope, element, attrs, ngModel) {

            function read() {
                ngModel.$setViewValue(element.text());
            }

            ngModel.$render = function () {
                element.html(ngModel.$viewValue || "");
            };

            element.bind("blur keyup change", function () {
                scope.$apply(read);
            });
        }
    };
});

Similar question: Cannot set property '$render' of undefined

Community
  • 1
  • 1

1 Answers1

0

I ended up with this:

link: function (scope, element, attrs, ngModel) {
    if (ngModel !== null) { //the check

so, self answered! Thanks!