Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

here is the fiddle to my problem http://jsfiddle.net/gxbwk6dk/7/

I have one service to find the element of a json , and I am calling that service from the controller twice but the results i am getting from both calls are the same.

in the example elementObj1 and elementObj2 has the same data

Any solution will be welcome.

var app=angular.module("myapp",[]);

app.controller("myctrl",function($scope,myservice)
{
console.log("add of two "+myservice.addTwo(5,7));

$scope.sum=myservice.addTwo(5,7);


$scope.sampleObj={
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
};
   myservice.findElement($scope.sampleObj,'GlossEntry').then(function(data){
     $scope.elementObj1=data;
	 console.log("find element object1 ", $scope.elementObj1);
    });
    
    
    myservice.findElement($scope.sampleObj,'GlossList').then(function(data){
     $scope.elementObj2=data;
	 console.log("find element object2 ", $scope.elementObj2);
    });



}
);


app.factory("myservice",function($q,$timeout){
var deferred = $q.defer();
return{
	addTwo:addTwo,
	findElement:findEle,
	sample:sample
	
};

function sample(jsObject)
{
var deferred = $q.defer();
$timeout(function(){deferred.resolve(jsObject)},5000);
  
return deferred.promise;

}

	function addTwo(a,b)
	{
		return a+b;
	}

function findEle(jsObject,searchEle)
	{
         
	for(obj in jsObject)
	{
		console.log("obj "+obj+" mapobj "+jsObject[obj]);
		if(obj===searchEle)
		{
			console.log("element found "+obj);
             deferred.resolve(jsObject[obj]);
			
		}
		if(typeof jsObject[obj]==="object")
		findEle(jsObject[obj],searchEle);
			
	}
         return deferred.promise;
	}


});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myapp" ng-controller="myctrl">
<input type="text" ng-model="name" />
<h2 ng-bind="name"></h2>
    
display object 1
<table>
<tr ng-repeat="(key, value) in elementObj1">
  <td> {{key}} </td> <td> {{ value }} </td>
</tr>

</table>
    
display object 2
 
<table>
<tr ng-repeat="(key, value) in elementObj2">
  <td> {{key}} </td> <td> {{ value }} </td>
</tr>

</table>
    
    
</body>

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Here are one working solution: http://jsfiddle.net/gxbwk6dk/9/

You are returning same object, I have modified when you have a hit:

if (obj === searchEle) {
  deferred = $q.defer();
  deferred.resolve(jsObject[obj]);
}
share|improve this answer
    
Thank you so much @Johnny Ha – thaveethu gce Jul 1 at 9:57
    
Your are welcome @thaveethu gce – Johnny Ha Jul 3 at 11:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.