View gist:36c8d1d246a94098c16b

This afternoon I encountered a race condition in an Angular app I'm working on. Essentially my controller was pushing some values to an Array on its scope and something (I wasn't sure what) was asynchronously overriding the Array's contents. The Array was being used by a custom directive- written by someone else- as well as an ngModel and it wasn't clear who was making the change.

I ended up trying something I had not done before and it worked well enough that I thought I'd post it here in case it helped anyone else.

First I enabled The "Async" option in Chrome's "Sources > Call Stack" panel.

Next I set a breakpoint in my controller where I was modifying the Array. When I hit that breakpoint, I ran the following code in my console:

Object.observe(this.theArray, function(changes) {
View slider.ts
slider.$inject = [];
function slider() {
return <ng.IDirective>{
restrict: 'E',
replace: true,
scope: {
model: '=ngModel',
isRequired: '=',
min: '=',
View numericOnly.ts
numericOnly.$inject = ['$log'];
function numericOnly(
$log: ng.ILogService): ng.IDirective {
return <ng.IDirective>{
restrict: 'A',
scope: true,
link: link
};
View datepickerFormatter.js
function datepickerPopup() {
return <ng.IDirective>{
restrict: 'EAC',
require: 'ngModel',
link: function ($scope, $element, $attrs, controller) {
controller.$formatters.shift();
}
}
}
angular.module('app').directive('datepickerPopup', datepickerPopup);
View sortableGrid.js
sortableGrid.$inject = [];
function sortableGrid() {
return <ng.IDirective>{
restrict: 'A',
scope: {
order: '=',
by: '=',
reverse: '='
},
View scoreDirective.js
scoreInput.$inject = [];
function scoreInput() {
return <ng.IDirective>{
restrict: 'E',
replace: true,
scope: {
model: '=ngModel',
isRequired: '=',
isReadonly: '=',
View index.html
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@*" data-semver="1.3.5" src="https://code.angularjs.org/1.3.5/angular.js"></script>
<script src="https://code.angularjs.org/1.3.5/angular-route.js" data-semver="1.3.5" data-require="angular-route@*"></script>
<script data-require="angular-resource@1.3.5" data-semver="1.3.5" src="https://code.angularjs.org/1.3.5/angular-resource.js"></script>
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
View service.js
angular.module("exampleApp", []).service('exampleService', ["$http", "$q" ,function ($http, $q) {
var service = {
returnedData: [],
dataLoaded:{},
getData = function(forceRefresh)
{
var deferred = $q.defer();
if(!service.dataLoaded.genericData || forceRefresh)
{
View controller.js
(function(app) {
app.controller('Ctrl', Ctrl);
app.controller('CtrlModal', CtrlModal);
//Ctrl.$inject = ['$scope', '$modal', 'dataservice'];
function Ctrl($scope, $modal, dataservice) {
var vm = this;
View sample_cte_paging.sql
-- Calculate paging
DECLARE @topRecord VARCHAR(20) = CAST(((@pageSize+@offset)+1) AS VARCHAR)
DECLARE @cte_query NVARCHAR(MAX)
SET @cte_query = WITH cte_res
AS ( SELECT ROW_NUMBER() OVER(ORDER BY ['+@sortBy+'] '+@sortDir+') AS rowid, count(*) over() as TotalRecords,* from (SELECT * FROM #someTable WITH(NOLOCK)) as table1)
select rowID, TotalRecords, id, someField from cte_res with(nolock) where rowid>'+ CAST(@offset AS VARCHAR) +' and rowid< ' + @topRecord
EXECUTE sp_executesql @cte_query