简体   繁体   中英

Angular.js canceling $http requests inside bind() event

According to this https://github.com/angular/angular.js/issues/1159

this should work, shouldn't it?

el.bind('keyup', function() {       
    var canceler = $q.defer();
    $http.post('/api', data, {timeout: canceler.promise}).success(success);
    canceler.resolve();
});

because it doesn't fire the request at all, no errors or anything, could it be because it's inside a bind function?

it indeed was because it's inside the nonangular bind() event, putting scope.$apply() after http and before resolve will fix it

https://github.com/angular/angular.js/issues/1159#issuecomment-20368490

el.bind('keyup', function() {       
    var canceler = $q.defer();
    $http.post('/api', data, {timeout: canceler.promise}).success(success);
    scope.$apply();
    canceler.resolve();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM