无法设置未定义的属性'$ render'
请为此错误提供帮助“无法设置属性'$ 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>
如果您在链接功能使用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);
});
}
需要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详细的解释。
然后我得到这个错误:错误:[$ compile:ctreq] Controller'ngModel',要求由指令'contenteditable',无法找到! –
@IvanLuketić纠正我,如果我错了,你想让我们的价值[key.name]可编辑的权利? –
是的,我想让它可编辑 –
然后我得到这个错误:错误:[$ compile:ctreq]无法找到指令'contenteditable'所需的控制器'ngModel'! –
@IvanLuketić看到这个:http://stackoverflow.com/a/21807863/1562490 –
所以我错过ng模型的某处 –