【问题标题】:Making a get request in directive - Ionic Framework在指令中发出获取请求 - Ionic Framework
【发布时间】:2015-04-15 16:33:39
【问题描述】:

我正在尝试调整 angular-pickdate (https://github.com/jimibi/angular-pickadate) 模块以满足我的需求,但由于无法按照我习惯的方式发出 GET 请求,因此我陷入了停顿。

我的代码是: ...

.directive('pickadate', ['$locale', 'pickadateUtils', 'pickadateI18n', 'dateFilter', function($locale, dateUtils, i18n, dateFilter) {


  return {
    require: 'ngModel',
    scope: {
      date: '=ngModel',
      defaultDate: '=',
      minDate: '=',
      maxDate: '=',
      disabledDates: '='
    },
    template:
      ...,

    link: function(scope, element, attrs, ngModel,$http)  {

      var minDate       = scope.minDate && dateUtils.stringToDate(scope.minDate),
          maxDate       = scope.maxDate && dateUtils.stringToDate(scope.maxDate),
          disabledDates = scope.disabledDates || [],
          currentDate   = (scope.defaultDate && dateUtils.stringToDate(scope.defaultDate)) || new Date();

      scope.dayNames    = $locale.DATETIME_FORMATS['SHORTDAY'];
      scope.currentDate = currentDate;
      scope.t           = i18n.t;
      scope.render = function(initialDate,$http) {
        initialDate = new Date(initialDate.getFullYear(), initialDate.getMonth(), 1, 3);

          $http({url: 'myplace/script_lotacoes.php', method: 'GET'}).success(function(){alert("hheheh");}).error(function(){alert("oops");}); 

...

这段代码给我的错误是:

  TypeError: undefined is not a function
at Scope.angular.module.provider.factory.directive.link.scope.render (angular-pickadate.js:122)
at angular.module.provider.factory.directive.link.ngModel.$render (angular-pickadate.js:191)
at Object.ngModelWatch (ionic.bundle.js:31576)
at Scope.$get.Scope.$digest (ionic.bundle.js:22518)
at Scope.$get.Scope.$apply (ionic.bundle.js:22789)
at done (ionic.bundle.js:17942)
at completeRequest (ionic.bundle.js:18132)
at XMLHttpRequest.requestLoaded (ionic.bundle.js:18073)  

此错误与 $http 请求有关。

有没有人知道我为什么会收到这个错误,我该如何解决? 提前致谢

【问题讨论】:

  • 试试$http.get('myplace/script_lotacoes.php').then(function() { alert('hheheh'); });
  • 我做到了。它给了我以下错误:ypeError: Cannot read property 'get' of undefined at Scope.angular.module.provider.factory.directive.link.scope.render (angular-pickadate.js:123)...
  • 还可以尝试将$http 注入您的指令定义,而不是link 函数
  • 也这样做了,哈哈同样的错误......这很奇怪。
  • 您是否从link() 中删除了http 注入?

标签: javascript angularjs httprequest ionic-framework ionic


【解决方案1】:

您将$http 注入到您的link 函数和scope.render 函数中。删除这两个注入尝试 - 您只需将其注入到您的指令中。

.directive('pickadate', ['$locale', 'pickadateUtils', 'pickadateI18n', 'dateFilter', '$http', function($locale, dateUtils, i18n, dateFilter, $http) {

【讨论】:

    【解决方案2】:

    在指令中注入$http服务:

    .directive('pickadate', ['$http', '$locale', 'pickadateUtils', 'pickadateI18n', 'dateFilter', function($http, $locale, dateUtils, i18n, dateFilter) {
    

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 2019-01-26
      • 2019-07-07
      • 2018-05-30
      • 2021-07-02
      • 2019-12-15
      • 2022-06-24
      • 2013-09-14
      相关资源
      最近更新 更多