在指令中发出获取请求 - Ionic Framework
Making a get request in directive - Ionic Framework
我正在尝试调整 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
服务:
.directive('pickadate', ['$http', '$locale', 'pickadateUtils', 'pickadateI18n', 'dateFilter', function($http, $locale, dateUtils, i18n, dateFilter) {
您正在将 $http
注入您的 link
函数以及您的 scope.render
函数。删除这两次注入尝试 - 您只需要将其注入您的指令即可。
.directive('pickadate', ['$locale', 'pickadateUtils', 'pickadateI18n', 'dateFilter', '$http', function($locale, dateUtils, i18n, dateFilter, $http) {
我正在尝试调整 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
服务:
.directive('pickadate', ['$http', '$locale', 'pickadateUtils', 'pickadateI18n', 'dateFilter', function($http, $locale, dateUtils, i18n, dateFilter) {
您正在将 $http
注入您的 link
函数以及您的 scope.render
函数。删除这两次注入尝试 - 您只需要将其注入您的指令即可。
.directive('pickadate', ['$locale', 'pickadateUtils', 'pickadateI18n', 'dateFilter', '$http', function($locale, dateUtils, i18n, dateFilter, $http) {