Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

1.1.5: $http delays ajax until next click when within a directive referencing a service. #2849

Closed
clouddueling opened this issue Jun 1, 2013 · 1 comment

Comments

@clouddueling
Copy link
Contributor

I'm using 1.1.5 unstable branch.
When I click the link it doesn't fire off the $http request.
When I click a $http request element directly after both fire at same time.

Example: http://plnkr.co/edit/wpA2Oa0y1pKSWv8kR3AP?p=preview

                    <a
                        ng-click='copyContact()'
                        ng-popup='/apps/tri/views/contacts/modals/update.html'>
                        Edit Details
                    </a>
directives.directive('ngPopup', function(Popup){
    return {
        restrict: 'A',
        link: function postLink(scope, element, attrs) {
            var ngPopupUrl = attrs['ngPopup'];
            // Could have custom or boostrap modal options here
            var popupOptions = {};

            element.bind( "click", function(){
                Popup.load(ngPopupUrl, scope, popupOptions);
            });
        }
    };
});
services.factory('Popup', function ($http, $compile) {
    // Got the idea for this from a post I found. Tried to not have to make this
    // object but couldn't think of a way to get around this
    var popupService = {};

    // Loads the popup
    popupService.load = function(url, scope, options) {
        $http.get(url).success(function (data) {
            l(1);
            var popup = popupService.getPopup(true);

            popup.html(data);
            popupService.compileAndRunPopup(popup, scope, options);

            popup.find(".btn-cancel").click(function () {
                popupService.close();
            });
        });
    }
});

https://github.com/clouddueling/angularjs-modals

@pkozlowski-opensource
Copy link
Member

@clouddueling This is a side-effect of promise-based request interceptors introduced in 1.1.4. Now request interceptors are promise-based and AngularJS promises are only resolved within $digest cycle. As from 1.1.4 you need to trigger $http / $resource requests from AngularJS $digest loop.

In your particular case you need to wrap a call to the Popup.load(ngPopupUrl, scope, popupOptions); into $scope.$apply since you are triggering this call outside of the "AngularJS world".

See those 2 comments in #2431 for additional info:
#2431 (comment)
#2431 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants