我正在使用可编辑表 jQuery插件来制作一个可编辑的表。它容易使用,精益和非常有效。
标记如下:
<tr ng-repeat="o in orders">
<td ng-bind="o.name"></td>
<td ng-bind="o.weight"></td>
<td ng-bind="o.dimensions"></td>
</tr>该表可通过以下方式进行编辑:
$('#ordertable').editableTableWidget();它运行得很好:表显示得很好,而且是可编辑的。如果数据模型更改,表也会被刷新。
但是,因为我使用ng-bind,所以绑定是单向的,并且表中的更改不会自动传播到数据模型。ng-model不能工作,因为它不是输入字段。
因为我对角度不熟悉,所以我不知道如何正确地更新我的数据模型。我可以用普通的JS来做,但是我想用角度的方式来做。
是将更改传播回模型的正确方法?
发布于 2016-06-16 09:39:23
我认为你不应该混合角度的方式更新值和可编辑插件。角不知道插件和其他,所以他们不能一起工作那样。
只需尝试在没有插件的情况下完成:
<tr ng-repeat="o in orders"> <td><input type="text" ng-model="o.name" /></td> <td><input type="text" ng-model="o.weight" /></td> <td><input type="text" ng-model="o.dimensions" /></td> </tr>
发布于 2016-06-16 09:38:39
每当使用jquery模型进行任何更改时,都不会自动更新,因为角不知道所做的更改,摘要周期也不会触发,因此可以显式地触发摘要周期,如下所示
$scope.$digest();
//but this will event not work因此,如果使用jquery,则要更新模型,还必须显式地为模型赋值。
发布于 2016-06-16 09:47:51
解决这个问题还有另外一种方法。与其使用jquery的可编辑表插件,不如在表单元格中使用“contenteditable”,使用ng模型而不是ng绑定。并可以为此内容编写可更新模型的指令。在本页末尾的AngularJS站点中有一个示例,https://docs.angularjs.org/api/ng/type/ngModel.NgModelController。
我从那里复制了指令代码,并相应地更改了您的html。
directive('contenteditable', ['$sce', function($sce) {
return {
restrict: 'A', // only activate on element attribute
require: '?ngModel', // get a hold of NgModelController
link: function(scope, element, attrs, ngModel) {
if (!ngModel) return; // do nothing if no ng-model
// Specify how UI should be updated
ngModel.$render = function() {
element.html($sce.getTrustedHtml(ngModel.$viewValue || ''));
};
// Listen for change events to enable binding
element.on('blur keyup change', function() {
scope.$evalAsync(read);
});
read(); // initialize
// Write data to the model
function read() {
var html = element.html();
// When we clear the content editable the browser leaves a <br> behind
// If strip-br attribute is provided then we strip this out
if ( attrs.stripBr && html == '<br>' ) {
html = '';
}
ngModel.$setViewValue(html);
}
}
};
}]);
<tr ng-repeat="o in orders">
<td contenteditable ng-model="o.name"></td>
<td contenteditable ng-model="o.weight"></td>
<td contenteditable ng-model="o.dimensions"></td>
</tr>
https://stackoverflow.com/questions/37854875
复制相似问题