I've made a couple of directives to make the bootstrap popovers happen when a form field is invalid. The only hurdle I'm having was attempting to 'evaluate' an attribute value to either true or false so i can call open or close the popover. I'm an angular neophyte (just learning) but doing some research, it seemed using $scope.watch is the thing to use (and also from looking at ng-show and ng-hide code). So everything works darling until I try to call element.triggerHandler() from within $scope.watch and using bootstrap.ui.
Here is my directive:
app.directive('tooltipTriggerOn', ['$log', function($log) {
function link(scope, element, attrs) {
scope.$watch(attrs.tooltipShow, function(val) {
if (val) {
$log.info('trigger openPopup');
} else {
$log.info('trigger closePopup');
}
//if (val) element.triggerHandler('openPopup');
//else element.triggerHandler('closePopup');
});
}
return {
restrict: 'A',
link: link
};
}]);
The if/else commented out makes the app run with no issues. If I enable those lines, I get javascript errors:
Error: [$rootScope:inprog] $digest already in progress
Error: a.$apply(...) is not a function
But why? This little scheme works fine when I'm not using ui.bootstrap. So:
- Is there a trick to use scope.$watch in my directive and not get the error?
- Is there a different way to 'evaluate' the directive attribute without using scope.$watch?
- Why is this error happening?
Here are the plunkrs:
This one demonstrates the issue with bootstrap.ui, just uncomment the offending if statement as described in the question.
This one demonstrates the $scope.watch working without bootstrap.ui
Any help appreciated!
- Mike