I have a form that is wired into angular, using it for validation. I am able to display error messages using ng-show directives like so:
<span ng-show="t3.f.needsAttention(f.fieldName)" ng-cloak>
<span ng-show="f.fieldName.$error.required && !f.fieldName.$viewValue">
This field is required.
</span>
</span>
.. where f
is the form, and t3
comes from a custom directive on the form which detects whether a submission was attempted, and contains functions for checking the validity of fields.
What I am trying to accomplish is to display validation message(s) inside a popover instead. Either bootstrap's native popover, or the popover from UI Bootstrap, I have both loaded. I may also consider AngularStrap if it is easier to do it using that lib.
What I'm struggling with right now is the nature of popovers in general -- they autodisplay based on user events like click, mouseenter, blur, etc. What I want to do is show & hide the popover(s) based on the same functions in the ng-show attributes above. So that when the expression returns false hide it, and when it returns true, show it.
I know bootstrap has the .popover('show') for this, but I'm not supposed to tell angular anything about the dom, so I'm not sure how I would get access to $(element).popover() if doing this in a custom form controller function. Am I missing something?
Update
The solution mentioned in the duplicate vote still only shows the popover on mouseenter. I want to force it to display, as if doing $('#popover_id').popover('show')
.
For others coming here, as of the 0.13.4 release, we have added the ability to programmatically open and close popovers via the
*-is-open
attribute on both tooltips and popovers in the Angular UI Bootstrap library. Thus, there is no longer any reason to have to roll your own code/solution.As it turns out, it's not very difficult to decorate either the ui-bootstrap tooltip or the popover with a custom directive. This is written in typescript, but the javascript parts of it should be obvious. This single piece of code works to decorate either a tooltip or a popover:
With this single piece of code, you can set up programmatic hide and show of either a tooltip or popover like so:
Usage:
or
So we are reusing everything else that comes with the ui-bootstrap tooltip or popover, and only implementing the
-toggle
attribute. The decorative directive watches that attribute, and fires custom events to show or hide, which are then handled by the ui-bootstrap tooltip provider.Update:
Since this answer seems to be helping others, here is the code written as javascript (the above typescript more or less compiles to this javascript):
For
ui.bootstrap
0.13.4 and newer:A new parameter (
popover-is-open
) was introduced to control popovers in the officialui.bootstrap
repo. This is how you use it in the latest version:For
ui.bootstrap
0.13.3 and older:I just published a small directive that adds more control over popovers on GitHub:
https://github.com/Elijen/angular-popover-toggle
You can use a scope variable to show/hide the popover using
popover-toggle="variable"
directive like this:Here is a demo Plunkr:
http://plnkr.co/edit/QeQqqEJAu1dCuDtSvomD?p=preview
You can also build your own extended triggers. This will apply to both Tooltip and Popover.
First extend the Tooltip triggers as follows:
Then define the trigger on the HTML tag like this:
And now you can call hide and show from JavaScript, this is a show in 3 seconds.
My approach:
The idea being to leave the DOM manipulation to the directives.
I have put together a fiddle that I hope gives a better explain, but you'll find much more sophisticated solutions in UI Bootstrap which you mentioned.
jsfiddle
Markup:
JS:
From Michael Stramel's answer, but with a full angularJS solution:
Now add this directive:
Then in your markup: