angular-strap icon indicating copy to clipboard operation
angular-strap copied to clipboard

Trigger manual pop for a popover

Open skytracer opened this issue 13 years ago • 20 comments

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

skytracer avatar Apr 19 '13 11:04 skytracer

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.

mgcrea avatar Apr 21 '13 22:04 mgcrea

Will close this for now, maybe we will come up with a $tooltip or $popover service in the future to perform manual openings. Thanks.

mgcrea avatar May 14 '13 17:05 mgcrea

Is the future near? Need this feature!

veeracs avatar Sep 05 '13 19:09 veeracs

mgcrea big thanks for your commant. I tried hours to show popover via javascript and your trick works!

moohkooh avatar Sep 20 '13 18:09 moohkooh

Would be nice to have this $popover factory!

m48u avatar Oct 24 '13 05:10 m48u

+1

GabrielDelepine avatar Mar 27 '14 23:03 GabrielDelepine

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');

                });
            }
        };
    })

;

j-walker23 avatar Mar 31 '14 05:03 j-walker23

Hi. I search for an example for manual trigger for popover without any success. Could you please provide a full example.

paulvitic avatar Apr 18 '14 14:04 paulvitic

+1

GabrielDelepine avatar Apr 18 '14 19:04 GabrielDelepine

Check 24c758c.

mgcrea avatar Apr 18 '14 21:04 mgcrea

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?

paulvitic avatar Apr 21 '14 13:04 paulvitic

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();
                    });

                }
            };
        })

    ;

j-walker23 avatar Apr 21 '14 15:04 j-walker23

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?

paulvitic avatar Apr 21 '14 16:04 paulvitic

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?

j-walker23 avatar Apr 21 '14 16:04 j-walker23

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 avatar Apr 22 '14 07:04 paulvitic

@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!

jacobjlevine avatar Apr 23 '14 05:04 jacobjlevine

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?

mparisi76 avatar Jun 18 '14 23:06 mparisi76

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. (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js:19093:21) at HTMLInputElement.jQuery.event.dispatch (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.js:4409:9) at HTMLInputElement.jQuery.event.add.elemData.handle (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.js:4095:28)

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' }); } })

DealerCorp avatar Sep 23 '14 20:09 DealerCorp

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);
}

bagadim avatar Apr 01 '16 14:04 bagadim

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.

lock[bot] avatar Jan 25 '19 23:01 lock[bot]