Reputation: 443
I have written a callback method in angularjs. But somehow, it is not called. My code is as follows.
Link function:
link: function(scope, element, attrs) {
scope.getContent = function(itemId) {
getTocService.getArtData(itemId, function(data){
var art = data;
alert(art);
});
}
element.append("<collection collection='member.tocItem'></collection>");
$compile(element.contents())(scope)
}
Service:
app.service( "getTocService", function( $http, $q ) { return({
getArtData: getArtData
});
function getToc(bookIdvar) {
var request = $http({
method: "post",
url: "http://10.132.241.41:8082/apdpoc/services/ApdBookService/getTOC",
params: {
action: "post"
},
data: {
getTOCCriteria:{
bookId: bookIdvar
}
}
});
return( request.then(handleSuccess,handleError));
}
function getArtData(itemId, cb) {
var request = $http({
method: "post",
url: "http://10.132.241.41:8082/apdpoc/services/ApdBookService/getArticle",
params: {
action: "post"
},
data: {
getArticleCriteria:{
articleId: itemId,
locale: "en_US"
}
}
});
return(request.then(handleSuccess,handleError), cb);
}
function handleSuccess(response){
return (response.data);
}
function handleError( response ) {
if (
! angular.isObject(response.data) ||
! response.data.message
) {
return($q.reject("An unknown error occurred."));
}
return($q.reject(response.data.message));
}
}
);
As per my little knowledge on angularJs, the call back method should execute
var art = data; alert(art);
these lines. but control is not coming over there. Can someone let me know what is the problem here?
Upvotes: 1
Views: 430
Reputation: 28455
You should update
return(request.then(handleSuccess,handleError), cb);
to
return(request.then(cb,handleError));
Upvotes: 1