Our team is developing in ServiceNow and have a requirement to add rows from "Sending" table to "Receiving" table and delete rows from the "Receiving" table and return it back to the "Sending":
We've nailed down the adding portion of this exercise, but am having trouble getting the returning part to work correctly. Sometimes it will work completely fine. Other times, when we delete, it would uncheck the correct entry from the "Sending" table, but the entry would remain on the "Receiving" table.
On our "Sending" table, it is receiving the rootScope like this:
<tbody>
<tr ng-repeat="item in data.list track by item.sys_id">
<td ng-if="options.show_checkboxes">
<input type="checkbox"
ng-checked="item.isRowSelected"
ng-click="toggleSelection(item);" >
</td>
<td role="cell" class="pointer sp-list-cell"
ng-class="{selected: item.selected}"
ng-click="go(item.targetTable, item)"
ng-repeat="field in ::data.fields_array" data-field="{{::field}}"
data-th="{{::data.column_labels[field]}}">
<span ng-if="$first"
aria-label="${Open record}: {{::item[field].display_value}}"
role="link" tabindex="0">
{{::item[field].display_value | limitTo : item[field].limit}}
{{::item[field].display_value.length > item[field].limit ? '...' : ''}}
</span>
<span ng-if="!$first">
{{::item[field].display_value | limitTo : item[field].limit}}
{{::item[field].display_value.length > item[field].limit ? '...' : ''}}
</span>
</td>
</tr>
</tbody>
$scope.selectedItems = [];
$scope.toggleSelection = function(item){
item.isRowSelected = !item.isRowSelected;
if(item.isRowSelected==false){
$scope.allSelected=false;
if($scope.selectedItems.indexOf(item.sys_id)!==-1){
var add = $scope.selectedItems.indexOf(item.sys_id);
$scope.selectedItems.splice(add,1);
}
} else {
if($scope.selectedItems.indexOf(item.sys_id)==-1){
$scope.selectedItems.push(
item
);
$rootScope.$broadcast('moveItem', $scope.selectedItems);
}
}
}
$rootScope.$on('deleteItem', function(event, data) {
$scope.selectedItems = data;
});
In our "Receiving" table widget, our code looks like this:
<tbody>
<tr ng-repeat="item in data.list2 track by item.sys_id">
<td role="cell" class="pointer sp-list-cell"
ng-class="{selected: item.selected}"
ng-click="go(item.targetTable, item)"
ng-repeat="field in ::data.fields_array"
data-field="{{::field}}" data-th="{{::data.column_labels[field]}}">
<span ng-if="$first"
aria-label="${Open record}: {{::item[field].display_value}}"
role="link" tabindex="0">
{{::item[field].display_value | limitTo : item[field].limit}}
{{::item[field].display_value.length > item[field].limit ? '...' : ''}}
</span>
<span ng-if="!$first">
{{::item[field].display_value | limitTo : item[field].limit}}
{{::item[field].display_value.length > item[field].limit ? '...' : ''}}
</span>
</td>
<td>
<a href="javascript:void(0)" ng-click="deleteSelection(item);">
Remove
</a>
</td>
</tr>
</tbody>
$scope.deleteSelection = function(item){
item.isRowSelected =false;
var minus = $scope.data.list2.indexOf(item.sys_id);
$scope.data.list2.splice(minus, 1);
$rootScope.$broadcast('deleteItem', $scope.data.list2);
}
$rootScope.$on('moveItem', function(event, data) {
$scope.data.list2 = data;
$scope.data.row_count = data.length;
});

Avoid
$rootScope.$on— it risks memory leaksIn the course of its operation, AngularJS adds and removes DOM with their attached directives and controllers. The listener functions added by
$rootScope.$onare not automatically removed when a directive or controller is removed. This can result in memory leaks and undesired behavior.To avoid memory leaks, add event listeners to the
$scopeof the controller, not$rootScope:To avoid injecting
$rootScope, use$scope.$root: