On load of page, I am making a restful call and getting data. This is working fine. On click of these data (which are left navigation links) I need to make another rest call and get data. I am trying to do that from link function. Since I am new to angular js, I am not sure how good/bad it is to call from there. I tried with several examples on how to call restful web service from link function but failed to implement successfully.
My js file code is as follows:
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));
}
on click of left nav links, I am not getting any error in browser console but its not hitting service . getContent() is the method I am trying to call on click of left nav links. Can someone please help me on this?
Edited:
Now, I am able to hit the rest service but getting "server responded with a status of 400 (Bad request)" . Request sent is
{
"getArticleCriteria" :{
"articleId": "PH1234",
"locale": "en_US"
}
}
This is the expected request and I am able to get response in Soap UI for the same request. Any changes I need to make while calling the rest service?
This is a cleaned up code with injection of $http service into directive
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));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.