I am trying to create a directive. Things just didn’t work, so I simplified the situation until I found this basic problem with the conditions of the race, causing problems for me. In my control controller, I need to do some checks, for example ...
if ($scope.test.someValue === true) { $scope.test.anotherValue += 1; }
Here is my main directive with some magazines to illustrate how this problem manifests itself.
app.directive('myDirective', function () {
return {
restrict: 'E',
replace: true,
scope: {
test: '='
},
template: '<div><pre>{{ test | json }}</pre></div>',
controller: function ($scope, $timeout) {
console.log($scope.test);
$timeout(function() {
console.log($scope.test);
}, 300);
}
};
});
What is the correct way to deal with this race condition? I am worried that in the real world this timeout function will work or not work depending on how long the api call will take.
source
share