Trigger manual pop for a popover
I am trying to get a manual trigger for a popover, because I want to pop it in response to a http request data call.
However, I get a $apply in progress error. If I add !$$scope.phase for the scope.$digest(); the popover position is not recalculated
Plunker here (only the manual trigger)
http://plnkr.co/edit/AbEmSsmZB9bJRaczOMhd
Thanks
Looks like wrapping it with $timeout does the trick:
$timeout(function() {
angular.element($event.currentTarget).popover('show');
});
We may want to find a easier way to achieve this.
Will close this for now, maybe we will come up with a $tooltip or $popover service in the future to perform manual openings. Thanks.
Is the future near? Need this feature!
mgcrea big thanks for your commant. I tried hours to show popover via javascript and your trick works!
Would be nice to have this $popover factory!
+1
I have an issue where i show a tooltip on a submit button only when the form is invalid. Something like "Hey user, don't you see all of the red fields in the form, this can't be submitted yet". I wrote a little directive that does this conditionally for a tooltip. I am sure it would be the exact same for popover
angular.module('ui.manual-tooltip', [])
.directive('uiManualTooltip', function ($parse) {
return {
require: '^form',
link: function (scope, element, attrs, form) {
element.hover(function(evt) {
if (form.$invalid)
element.tooltip('toggle');
else
element.tooltip('hide');
});
}
};
})
;
Hi. I search for an example for manual trigger for popover without any success. Could you please provide a full example.
+1
Check 24c758c.
Thank you for the sample. I must say that I am new to Angular and finding the learning curve steep. I would be grateful if you could help me out with an example for the scenario below. I have a password input in a form. Currently a warning appears in a div when I focus on the input and "form.password.$dirty && form.password.$invalid". The message updates as user types the password and depends on error type and is translated. It disappears on blur or when password is no longer invalid. All this is done with ng-show and translate directives in markup. I would like to achieve the same with a popover rather than the message appearing in a div. Is this at all possible with angular-strap popover?
This is untested but hopefully is a start to what you are looking for. I don't have a lot of time, but later tonight i can provide more detail if needed.
angular.module('ui.manual-popover', [
'mgcrea.ngStrap.popover'
])
.directive('uiManualPopover', function ($popover) {
return {
require: '^form',
link: function (scope, element, attrs, form) {
var ts = $popover(element, {
title: attrs.title,
animation: attrs.animation,
trigger: 'manual'
});
scope.form = form;
// watch the validity on the form element you care about
scope.$watch('form.password.$invalid', function(value, old) {
// whatever logic checking here you need to do
if (form.$invalid)
ts.toggle();
else
ts.hide();
});
}
};
})
;
Thank you j, yes it is a good start. The next hurdle is to set the content of the popover dynamically. based on error type, i.e. form.password.$error.required or form.password.$error.minlength etc. Would it be possible to for you to have a go at this?
Will the content be a one liner, or would you want a separate template for the content?
If its a one liner it would be easy, just pass it in as an attribute to the html element the custom popover directive is on.
something like
<input ui-manual-popover error="my error content" />
var ts = $popover(element, {
title: attrs.title,
content: attrs.error, // this is "my error content"
animation: attrs.animation,
trigger: 'manual'
});
Is this what you are thinking?
Not exactly. The error content is dynamic. Is changes depending on the error type (also translated with translate directive but I will deal with the translation later.). So I guess I have to watch changes in some scope parameter such as "form.password.$error" (in fact I probably have to watch multiple scope parameters and link to element focus and blur events as well, but I will test these later too). At this point I am trying to update popover content dynamically as the user types the password in.
The mark up of the div that I am trying to emulate with a popover is as follows:
<div class="alert alert-danger alert-block" ng-show="form.password.$dirty && form.password.$invalid">
<small ng-show="form.password.$error.required"
translate="password.messages.validate.newpassword.required">
Your password is required.
</small>
<small ng-show="form.password.$error.minlength"
translate="password.messages.validate.newpassword.minlength">
Your password is required to be at least 5 characters
</small>
<small ng-show="form.password.$error.maxlength"
translate="password.messages.validate.newpassword.maxlength">
Your password cannot be longer than 50 characters
</small>
</div>
I tried to change your example slightly to include a contentTemlape as follows:
angular.module('myApp')
.directive('manualPopover', function ($popover) {
return {
require: '^form',
link: function (scope, element, attrs, form) {
var ts = $popover(element, {
contentTemplate: 'views/partials/passwordError.html',
trigger: 'manual'
});
scope.form = form;
scope.$watch('form.password.$invalid', function(value, old) {
if (form.password.$invalid) {
ts.toggle();
}else {
ts.hide();
}
});
}
};
});
using above markup for passwordTemplate but it did not work. The popover appears without any content.
@paulvitic I encountered the same problem (the popover appeared without content), but I just discovered the problem for me had to do with scope: the entire template relies on scope variables, and I wasn't careful about making sure the right scope got attached to the directive (since none of my scope variables existed in the directive's scope, it appeared blank).
Not sure if that has any relevance for your problem, but thought I'd throw it out there just in case (for example, it might be impacting the ng-show in the parent div). Good luck!
I'm running into the same issue. Can anyone post a solution? I have a popover using a template which contain bindings and I have verified that the scope on the directive includes the variables which the template uses. How do I apply the scope variable values to the popover template as they are not being populated automatically?
I failed to manually trigger popover.
run into this error:
TypeError: undefined is not a function
at Scope.$scope.test (bz.bos.js:4:22)
at Parser.functionCall (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js:10836:21)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js:19094:17
at Scope.$get.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js:12673:28)
at Scope.$get.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js:12771:23)
at HTMLInputElement.
Here is my test code: input id="test" type="text" ng-click="test($event)"
function testCtrl($scope, $popover) { $scope.test = function (evt) { $popover(document.querySelector('#lname'), { template: 'popover/test.html' }); } })
Hello. Faced similar problem while showing triggered popup on page start. In my case bs-show was triggered before bs-popover was rendered. Timeout solution worked for me:
index.html:
<div bs-popover data-title="Test" bs-show="$ctrl.showPopover"> </div>
controller.js:
function testController($timeout) {
this.showPopover = false;
$timeout(function() {
this.showPopover = true;
}.bind(this), 50);
}
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.