1

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?

1 Answer 1

1

You should update

return(request.then(handleSuccess,handleError), cb);

to

return(request.then(cb,handleError));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. it is working. Can you please let me know whether I can assign value to $scope variable inside link function?
Good to hear that. Yes you can do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.