我正在尝试使用我的控制器方法中的参数调用服务,并且我收到一条错误消息,指出它不是函数。这是我的代码:
(function (){
angular.module('app.uploadedReleases')
.controller('UploadedReleasesController', UploadedReleasesController)
.controller('ModalController', ModalController);
UploadedReleasesController.$inject = ['$log', '$scope', '$modal', 'ReleaseService', 'TrackService'];
function UploadedReleasesController ($log, $scope, $modal, releaseService, trackService){
vm.selectTrack = selectTrack;
function selectTrack(album, track, index){
trackService.currentTrack(album.slug, track.fileName).then(function(responseValue){
vm.temp = responseValue;
});
vm.formTrack = vm.selected.track;
}
}
这是我的服务,名为trackService:
angular.module('app.services')
.service('TrackService', TrackService);
TrackService.$inject = ['$http', '$q'];
function TrackService($http, $q){
var trackService = {};
var releasesUrl = 'http://localhost:8080/api/releases';
trackService.currentTrack = currentTrack;
function currentTrack(releaseSlug, trackSlug){
var trackUrl = releasesUrl + '/'+ releaseSlug + '/' + trackSlug + '/track';
var deferred = $q.defer();
$http.get(trackUrl).then(function(trackResponse){
return deferred.resolve(trackResponse);
}, function(error){
return deferred.reject({msg: 'Error while getting the current track details'})
});
return deferred.promise;
}
}
我知道为什么我会在控制器中收到错误:TypeError: trackService.currentTrack is not a function
答案 0 :(得分:3)
@Pankaj Parkar的回答是正确的,因为您应该从currentTrack
返回一个承诺,以便在您的控制器中链接then
。
但是,要回答有关TypeError的问题,请在TrackService
中混合出厂和服务语法。
要使其成为工厂,请返回已公开函数的地图:
function TrackService($http, $q){
var trackService = {};
trackService.currentTrack = currentTrack;
function currentTrack(releaseSlug, trackSlug){
//...
}
return trackService;
}
并使用.factory('TrackService', TrackService);
声明。
要使其成为服务,请将公开的函数添加到this
:
function TrackService($http, $q){
this.currentTrack = currentTrack;
function currentTrack(releaseSlug, trackSlug){
//...
}
}
并使用.service('TrackService', TrackService);
声明。
答案 1 :(得分:1)
我要说的第一件事是,你正在创建一个服务然后你应该将方法绑定到this
上下文,而不是创建对象。
var trackService = {};
会改为
var trackService = this;
但是从服务返回自定义对象也没错。所以在目前的情况下你错过了回来。在您的服务代码末尾添加
return trackService
仍然困惑如何服务&工厂工作,我建议你在this answer
上阅读您应该使用自定义方式创建的trackService.currentTrack
方法返回承诺。
trackService.currentTrack = currentTrack;
function currentTrack(releaseSlug, trackSlug){
var trackUrl = releasesUrl + '/'+ releaseSlug + '/' + trackSlug + '/track';
var deferred = $q.defer();
$http.get(trackUrl).then(function(trackResponse){
return deferred.resolve(trackResponse);
}, function(error){
return deferred.reject({msg: 'Error while getting the current track details'})
});
return deferred.promise; //returned promise
}
相反,你应该做你的自定义承诺,这被认为是在$http.get
方法返回承诺的情况下实现的反模式,你应该利用它。
重构版本
trackService.currentTrack = currentTrack;
function currentTrack(releaseSlug, trackSlug){
var trackUrl = releasesUrl + '/'+ releaseSlug + '/' + trackSlug + '/track';
return $http.get(trackUrl).then(function(trackResponse){
return trackResponse;
}, function(error){
return {msg: 'Error while getting the current track details'};
});
}