In my controller, I set a variable "data" to a default value. In my original project, I use CouchCorner to fetch data from a CouchDB and update data.
Within a derictive I watch the variable data and update the element accordingly. However, I ran in the problem of "$digest already in progress", which results that the derective is not updated correctly. I already found a lot of information on the $digest problem, but I couldn't find any answer which fits to my problem.
I created the following jsFiddle, which uses $timeout instead of an actual CouchDB request: http://jsfiddle.net/cwesM/4/
var App = angular.module('myApp', []);
function Ctrl($scope, $timeout) {
$scope.data="Initial";
$timeout(function(){
$scope.data="Updated"}, 1)
}
App.directive('chart', function(){
return{
restrict: 'E',
link: function(scope, elem, attrs){
scope.$watch(attrs.ngModel, function(v){
alert(v)
elem.html("Data="+v);
});
}
};
});
The script changes the initial value of "data" to "Updated" after 1ms. But the directive is not updated, although the $watch function is executed. Interestingly, if I remove the alert(), everything works as expected.
How I can avoid the collision of the $digest?