无法设置未定义的属性'$ render'

时间:2016-06-21 06:24:25

标签: javascript angularjs

请求帮助解决此错误“无法设置未定义的属性'$ render'”。我知道这个问题已被提出但我无法解决。

    $scope.editmode = false;
$scope.toggleEditMode = function () {
    $scope.editmode = $scope.editmode === false ? true : false;

}

    directives.directive("contenteditable", function () {

返回{

link: function(scope, element, attrs, ngModel) {

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

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

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

}; });

    <tr dir-paginate="us in zdravstveniPostupci |  filter: {Postupak: nasUser} | orderBy:orderByField:reverseSort | itemsPerPage:selectedMenu" ng-dblclick="updateOsoba(us, selectMjesto )" on-long-press="updateOsoba(us, selectMjesto )" ng-click="odaberiOsobe(us,$index)" ng-attr-contenteditable="{{ editmode }}">

                        <td ng-repeat="key in draggableObjects" ng-if="key.visible"  >
                            <span ng-if="key.name!='Mjesta'"> {{us[key.name]}}</span>

                        </td>

                    </tr>

    <div class="col-md-4 center-block" style="position:fixed;top:36%;left:82%;" id="gumbicc">
<button type="button" class="btn  btn-responsive"  ng-click="toggleEditMode(); ShowHide()" >{{editmode ? 'Disable Editing' : 'Enable Editing'}}</button></div>

2 个答案:

答案 0 :(得分:0)

如果您在链接功能中使用ngModel,则应在指令

中使用它
return {
   require: 'ngModel', // <---THIS
   link: function(scope, element, attrs, ngModel) {

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

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

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

答案 1 :(得分:0)

在您的指令中要求使用ngModel。

directives.directive("contenteditable", function () {
    return {
        require: 'ngModel',
        link: function(scope, element, attrs, ngModel) {

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

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

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

您可以参考此现有SO post以获取有关ngModel的详细说明。