I have a directive but I am facing problem in watching an attribute from the directive controller.
angular.module('app',[])
.directive('timer1', function () {
return {
template: '<div class="timerCont1"><div class="progressBar"></div></div>',
restrict: 'E',
replace:true,
scope: true,
controller: function ($scope, $element, $attrs) {
$scope.$watch($attrs.timerevent, function (value) {
switch ($attrs.timerevent)
{
case "start":
$scope.timeoutId = null;
$scope.countdown = Number($attrs.timer);
$scope.tick();
break;
case "stop":
$scope.stop();
break;
case "destroy":
alert()
$scope.stop();
$scope.$emit('destroy_pizza', {
});
}
},true);
$scope.tick = function () {
$scope.timeoutId = setTimeout(function () {
if ($scope.countdown <= 0) {
$scope.$apply(function () {
$attrs.$set('timerevent', 'destroy')
});
}
else {
$scope.countdown--;
$scope.tick();
}
$scope.$apply();
}, $attrs.interval);
};
$scope.stop = function () {
clearTimeout($scope.timeoutId);
$scope.timeoutId = null;
};
}
};
});
And here goes my hTML
<timer1 interval="1000" timerevent="start" timer="10"></timer1>
When I am setting the attribute timerevent to "destroy" my watch is not getting called, whereas the attribute is updated successfully.