Here is the trick.
Twitter prompts Bootstrap (for Angular-UI) it is possible to specify a trigger event with an additional attribute, as in data-trigger="mouseenter" . This gives you the ability to programmatically change the trigger (using Angular):
<input ng-model="user.username" name="username" tooltip="Some text" tooltip-trigger="{{{true: 'mouseenter', false: 'never'}[myForm.username.$invalid]}}" />
Therefore, when username not valid in $, the expression tooltip-trigger will evaluate to 'mouseenter' and a tooltip will appear. Otherwise, the trigger will evaluate to 'never' , which in turn does not trigger a tooltip.
EDIT:
@cotten (in the comments) mentions a scenario in which the tooltip gets stuck and does not disappear, even if the model is valid. This happens when the mouse stays above the input field during text input (and the model becomes valid). Once the model check evaluates to true, tooltip-trigger switch to never.
UI Bootstrap uses the so-called triggerMap to determine which mouse events show / hide the tooltip.
As you can see, this card does not know anything about the "never" event, so it cannot determine when to close the tooltip. So, to see the trick, we only need to update this map with our own pair of events, and UI Bootstrap then finds out which event to watch when the tooltip-trigger closes when the tooltip-trigger set to never.
app.config(['$tooltipProvider', function($tooltipProvider){ $tooltipProvider.setTriggers({ 'mouseenter': 'mouseleave', 'click': 'click', 'focus': 'blur', 'never': 'mouseleave'
Plunker
Note. The $ tooltip provider is set by the "ui.bootstrap.tooltip" module, and it allows us to globally configure tooltips in the application configuration.
Stewie May 20 '13 at 15:38 2013-05-20 15:38
source share