来自链接功能的宁静Web服务调用

时间:2016-01-04 10:33:22

标签: javascript jquery angularjs rest

在加载页面时,我正在进行一次宁静的调用并获取数据。这工作正常。点击这些数据(左侧导航链接),我需要进行另一次休息调用并获取数据。我试图通过链接功能来做到这一点。由于我是角度js的新手,我不确定从那里打电话有多好/坏。我尝试了几个关于如何从链接函数调用restful web服务但未能成功实现的示例。

我的js文件代码如下:

var app = angular.module('ngdemo', []);

app.directive('collection', function() {
    return {
        restrict: "E",
        replace: true,
        scope: {
            collection: '=',
            articleData: '=',
            articleContent: '='
        },
        template: "<ul><member ng-repeat='member in collection' member='member' article-data='articleData' article-content='articleContent'></member></ul>"
    }
});

app.directive('member', function($compile) {
    return {
        restrict: "A",
        replace: true,
        scope: {
            member: '=',
            articleData: '=',
            articleContent: '='
        },
        template: "<div><li><a href='#' ng-click="getContent(member.itemId)">{{member.title}}</a></li></div>",
        link: function(scope, element, attrs) {
            scope.getContent = function(itemId) {
                //scope.articleContent.content = articleData[0].getArticleResponse.articleDetail.articleContent;
                //scope.articleContent.title = articleData[0].getArticleResponse.articleDetail.title;


                    var request = $http({
                    method: "post",
                    url: "http://10.132.241.41:8082/apdpoc/services/ApdBookService/getArticle",
                    params: {
                        action: "post"
                    },
                    articleContents: {
                        getArticleCriteria:{
                            articleId: itemId,
                            locale: "en_US"

                        }
                    }
                });

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


            }

            if (angular.isArray(scope.member.tocItem)) {
                if (scope.member.hasChildren == "true") {
                    for (var i = 0; i < scope.member.tocItem.length; i++) {
                        if (scope.member.tocItem.title) {
                            scope.member.tocItem.title.hide = true;
                        }
                    }
                }
                element.append("<collection collection='member.tocItem'></collection>");    
                $compile(element.contents())(scope)
            }
        }
    }
});

app.controller('apdController', function($scope, getTocService) {
    var sampdata = getTocService.getToc('bookid-1');
   $scope.tasks =sampdata;

    $scope.articleContent = {};
});


app.service(
        "getTocService",
        function( $http, $q ) {
            return({
                getToc: getToc
            });

            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 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));
}

点击左侧导航链接,我没有在浏览器控制台中收到任何错误但是没有点击服务。 getContent()是我试图点击左侧导航链接时调用的方法。有人可以帮我这个吗?

编辑:

现在,我可以点击其余服务,但是“服务器响应状态为400(错误请求)”。发送的请求是

{
"getArticleCriteria" :{
            "articleId": "PH1234",
            "locale": "en_US" 
           }
           }

这是预期的请求,我可以在Soap UI中获得相同请求的响应。我在调用休息服务时需要做出哪些更改?

1 个答案:

答案 0 :(得分:0)

这是一个清理过的代码,将$ http服务注入指令

var app = angular.module('ngdemo', []);

app.directive('collection', function () {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            collection: '=',
            articleData: '=',
            articleContent: '='
        },
        template: '<ul><member ng-repeat="member in collection" member="member" article-data="articleData" article-content="articleContent"></member></ul>'
    }
});

app.directive('member', function ($compile, $http) { //NOTE THE INJECTED $http
    return {
        restrict: 'A',
        replace: true,
        scope: {
            member: '=',
            articleData: '=',
            articleContent: '='
        },
        template: '<div><li><a href="#" ng-click="getContent(member.itemId)">{{member.title}}</a></li></div>',
        link: function (scope, element, attrs) {
            scope.getContent = function (itemId) {
                //scope.articleContent.content = articleData[0].getArticleResponse.articleDetail.articleContent;
                //scope.articleContent.title = articleData[0].getArticleResponse.articleDetail.title;

                var request = $http({
                    method: 'post',
                    url: 'http://10.132.241.41:8082/apdpoc/services/ApdBookService/getArticle',
                    params: {
                        action: 'post'
                    },
                    articleContents: {
                        getArticleCriteria: {
                            articleId: itemId,
                            locale: 'en_US'

                        }
                    }
                });

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

            };

            if (angular.isArray(scope.member.tocItem)) {
                if (scope.member.hasChildren == 'true') {
                    for (var i = 0; i < scope.member.tocItem.length; i++) {
                        if (scope.member.tocItem.title) {
                            scope.member.tocItem.title.hide = true;
                        }
                    }
                }
                element.append('<collection collection="member.tocItem"></collection>');
                $compile(element.contents())(scope)
            }
        }
    }
});

app.controller('apdController', function ($scope, getTocService) {
    $scope.tasks = getTocService.getToc('bookid-1');

    $scope.articleContent = {};
});

app.service('getTocService',
    function ($http, $q) {
        return ({
            getToc: getToc
        });

        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 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));
}