在指令中发出get请求 - Ionic Framework

时间:2015-04-15 16:33:40

标签: javascript angularjs httprequest ionic-framework ionic

我正在尝试调整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请求有关。

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

2 个答案:

答案 0 :(得分:0)

在指令中注入$http服务:

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

答案 1 :(得分:0)

您正在将$http注入link函数以及scope.render函数。删除这两次注入尝试 - 您只需要将其注入您的指令。

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