The angular app receives data in an array. With that data, $compile is being used to create a angular directive. Example...
for(i = 0; i < trueTest.length; i++) {
var test = trueTest[i];
$scope.directiveData = {
blockId: test.blockId,
dataType: test.dataType,
type: test.type,
directiveType: test.directiveType
};
$compile("<my-directive block-id='{[{ directiveData.blockId }]}' block-data-type='{[{ directiveData.dataType }]}' block-type='{[{ directiveData.type }]}' block-directive-type='{[{ directiveData.directiveType }]}'></my-directive>")($scope.$new());
elem.find('.SomeElement').append(el);
}
Custom directive my-directive has an isolated scope with values blockId, blockDataType etc...
Most imprtant value is blockId inside my-directive. It determines what kind of data is going to be displayed in that directive. The problem is that it seems that $compile creates elements after the loop finishes so in every directive that $compile creates, isolated scope property is always that last one created in the loop.
My guess is that $compile creates elements (directives) asynchronously so by the time the loop finishes, the directives are only then created with the current values of $scope.directiveData. Or, directives are created as plain DOM elements but the scope is created afterwards, thus creating scopes of all directives with the last value of directiveData object.
Could anyone explain what is actually going on here?
EDIT
Here is a jsFiddle link of the problem. It doesn't run beacuse the code is quite large and i can't recreate the problem in fiddle but I tried to comment it as much as possible.
SECOND EDIT.
So i did some console.log(). I putted a console.log() right after elem.find('.SomeElement').append(el); and one console.log('just print this') inside the directives controller. It seems that elements are created in the DOM before the controller is of the directive is called. That would mean that all the DOM elements are created before the scopes are assigned.
Am i wrong?