简体   繁体   English

Angular库无法进行HTTP调用

[英]Angular library no making HTTP calls

i have build angular library using this Generator all the functions of the library works fine when i call them from the app but, the functions that make HTTP calls are not working. 我已使用此Generator生成了角度库,当我从应用程序调用它们时,库的所有功能都可以正常工作,但是进行HTTP调用的功能无法正常工作。 Is there any reason preventing from making HTTPS calls from an Angular library ! 有什么理由阻止从Angular库发出HTTPS调用!

Here is a code sample of the the library code: 这是库代码的代码示例:

(function (angular) {

  // Create all modules and define dependencies to make sure they exist
  // and are loaded in the correct order to satisfy dependency injection
  // before all nested files are concatenated by Gulp

  // Config
  angular.module('myLib.config', [])
      .value('myLib.config', {
          debug: true
      });

  // Modules
  angular.module('myLib.directives', []);
  angular.module('myLib.services', []);
  angular.module('myLib',
      [
          'myLib.config',
          'myLib.directives',
          'myLib.services'
      ]);

})(angular);

(function (angular) {
    'use strict';
    angular.module('myLib.services', [])
        .factory('myLib', myLib);

        myLib.$inject = ['$http', '$rootScope'];
        function myLib($http, $rootScope) {

            var myLibrary = {};

         myLibrary.register = function() {
          $http({
            method: 'POST',
            url: 'url', 
            data: {id: '00000000000'},
            headers: {'Authorization': 'Basic ' + credentialsEncoded}
          });
        };


            return myLibrary;
        }
})(angular);

when am trying to call the register function the HTTP call is not made however, call another functions is working fine. 当尝试调用注册函数时,尽管未进行HTTP调用,但调用其他函数仍能正常工作。

With $http(...) you create a so called promise. $http(...)创建一个所谓的promise。 To execute the query simply call 要执行查询,只需调用

$http({...}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

See the official Angular $http documentation for more information about the callbacks. 有关回调的更多信息,请参见Angular $ http官方文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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