gpt4 book ai didi

angularjs - 在回调事件中触发摘要循环

转载 作者:行者123 更新时间:2023-12-01 06:04:16 25 4
gpt4 key购买 nike

我想创建一个相应的 textarea 以及一个 handstontable,这样修改表格会对文本产生影响,反之亦然。这是一个 JSBin .

<!DOCTYPE html>
<html>
<head>
<script src="https://handsontable.github.io/ngHandsontable/node_modules/angular/angular.js"></script>
<script src="https://docs.handsontable.com/pro/1.8.2/bower_components/handsontable-pro/dist/handsontable.full.js"></script>
<link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/pro/1.8.2/bower_components/handsontable-pro/dist/handsontable.full.min.css">
<script src="https://handsontable.github.io/ngHandsontable/dist/ngHandsontable.js"></script>
</head>
<body ng-app="app">
<div ng-controller="Ctrl">
<hot-table settings="settings" on-after-create-row="onAfterCreateRow" datarows="dataJson"></hot-table>
<br><br>
<textarea cols=40 rows=20 ng-model="dataString"></textarea>
</div>
<script>
var app = angular.module('app', ['ngHandsontable']);
app.controller('Ctrl', ['$scope', '$filter', 'hotRegisterer', function ($scope, $filter, hotRegisterer) {
$scope.dataJson = [[5, 6], [7, 8]];

$scope.onAfterCreateRow = function (index, amount) {
$scope.$digest();
};

$scope.$watch('dataJson', function (dataJson_new) {
$scope.dataString = $filter('json')(dataJson_new);
}, true);

$scope.$watch('dataString', function (dataString_new) {
try {
$scope.dataJson = JSON.parse(dataString_new);
} catch (e) {
}
}, true);

$scope.settings = {
contextMenu: true,
contextMenuCopyPaste: {
swfPath: 'zeroclipboard/dist/ZeroClipboard.swf'
}
};
}]);
</script>
</body>
</html>

但是,我意识到的一件事是,添加/删除行/列不会触发 dataJSON 的观察者。 (而修改单元格值就可以了)。所以我必须使用 $scope.$digest()在诸如 onAfterCreateRow 的回调中以反射(reflect)添加行的变化。但它引发了 Uncaught TypeError: Cannot set property 'forceFullRender' of null :

screen shot 2017-02-01 at 17 07 55

$scope.$digest()在其他回调中(即 onAfterCreateColonAfterRemoveRowonAfterRemoveCol )将引发相同的错误。我认为这是一个严重的问题,如果我们不能很好地触发这些回调事件中的摘要循环。有谁知道如何解决这个问题或有任何解决方法?

最佳答案

$timeout是目前某些旧版本 angular 的解决方法,您可以使用 $applyAsync创建它是为了替换 $timeout 作为解决方法,并在您收到错误的情况下工作 $digest already in progress

关于angularjs - 在回调事件中触发摘要循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com