我是棱角分明的新人。我正在使用一个包含函数trelloService
的服务getTaskData
。在ng-click
上,调用控制器的函数addme()
,然后使用trelloservice.getTaskData
获取数据以便在页面上显示它。但是我得到了这个错误。
错误:
TypeError: trelloService.getTaskData is not a function
at Object.addme (scripts1.js:104)
at fn (eval at compile (angular.js:13231), <anonymous>:4:261)
at f (angular.js:23371)
at b.$eval (angular.js:15878)
at b.$apply (angular.js:15978)
at HTMLButtonElement.<anonymous> (angular.js:23376)
at HTMLButtonElement.dispatch (jquery.min.js:3)
at HTMLButtonElement.q.handle (jquery.min.js:3)
(anonymous) @ angular.js:12416
Js代码:
angular.module('myApp',[])
.service('trelloService', ['$http', '$q' ,function trelloService($http, $q) {
angular.extend(this, {getTaskData});
var deffered;
function getTaskData(index, title) {
deffered = $q.defer();
config = {
data: {
//data here
headers: {'Content-Type': 'application/json'}
}
$http.get('/api/task/create/1',config)
.then((response) => {
//
})
.catch((err) => {
//
})
return deferred.promise;
}
return getTaskData;
}])
.controller('parentcontroller', ['$compile','$http', 'trelloService' , function( $compile, $http, trelloService){
'ngInject';
var vm = this;
angular.extend(vm, {addme});
function addme(index){
trelloService.getTaskData(index, vm.title)
.then((response) => {
console.log("in then");
vm.title = data.title;
})
.catch((error) => {
console.log(error);
})
}
答案 0 :(得分:0)
函数 getTaskData 绑定
.service('trelloService', ['$http', '$q' ,function trelloService($http, $q) {
var deffered;
this.getTaskData = function(index, title) {
deffered = $q.defer();
config = {
data: {
//data here
headers: {'Content-Type': 'application/json'}
}
$http.get('/api/task/create/1',config)
.then((response) => {
deffered.resolve(response);
})
.catch((err) => {
deffered.reject(err);
})
return deffered.promise;
}
}])