Start digest cycle in callback events

I want to create an appropriate text box along with handstontable, so changing the table affects the text and vice versa. Here is the JSBin .

<!DOCTYPE html>
<html>
<head>
    <script src="https://handsontable.imtqy.com/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.imtqy.com/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>

However, one thing that I understand is that adding / removing rows / columns DOES NOT fire the observer dataJSON(whereas changing the value of the cell will do). Therefore, I should use $scope.$digest()in callbacks such as onAfterCreateRowto reflect the change in adding rows. But he raises Uncaught TypeError: Cannot set property 'forceFullRender' of null:

screen shot 2017-02-01 at 17 07 55

$scope.$digest() (.. onAfterCreateCol, onAfterRemoveRow onAfterRemoveCol), . , , . - , - ?

+4
2

$timeout angular, $applyAsync, $timeout , , $digest already in progress

0

$timeout, . . JSBin. , , $timeout, . , .

0

Source: https://habr.com/ru/post/1668468/


All Articles