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

I am really new to using AngularJS and Javascript. I need to get the totalResults data from a JSON source that looks like this:

{
 "queries": {
  "request": [
   {
    "totalResults": "51"
   }
  ]
 }
}

Once I get the data, I need it to show up in a list using AngularJS. I have tried really hard using .ajax and .getJSON, but I am unable to get either of them to work, given my complete lack of knowledge on using JavaScript. I really appreciate your help! My AngularJS looks like this:

function MyController($scope){
        $scope.url = "https://www.googleapis.com/customsearch/v1?key=[MYKEY]&cx=[MYSECRETKEY]&q=flowers&alt=json&fields=queries(request(totalResults))";
        $scope.newMessage = "";
        $scope.messages = ["Steve Jobs - 515,000,000 results"]; 

        $scope.add = function(){
            $scope.messages.push($scope.newMessage);
        };
        }
  }

In the HTML part, I have this:

<input type="text" ng-model="newMessage">
    <input type="submit" ng-click="add()" value="Click to find out!">

When the user clicks the button, I want the url to be called and the $scope.newMessage should be the value in totalResults. Thanks for your help!

share|improve this question
    
plnkr.co/edit/nZkYsxLHLvf3SZNiJKic – Prajoth Jan 24 '13 at 5:10
up vote 16 down vote accepted

You can use $http service: Documentation

So you can:

$scope.add = function(){
  $http.get($scope.url).then(function(response) {
            $scope.newMessage = response.data.queries.request.totalResults;
            $scope.messages.push($scope.newMessage);
  });
};
share|improve this answer
2  
I thought that callback receives response object, not the data directly. So perhaps it should be srvResponse.data.queries.request.totalResults? Or am I wrong? – ŁukaszBachman Jan 22 '13 at 20:44
    
Corrent. I'll edit answer. – Valentyn Shybanov Jan 22 '13 at 21:26
    
hey, this still isn't working for some reason. I tried adding an alert before the $scope.newMessage = response.data.queries.request.totalResults; part, but it's not getting called :( – Prajoth Jan 23 '13 at 15:52
    
This is the issue that Google Chrome is showing: TypeError: Cannot call method 'get' of undefined at Object.MyController.$scope.add (file://localhost/Users/Prajoth/Downloads/index%202.html:16:‌​17) at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angula‌​r.js:6129:19 at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angula‌​r.js:12496:13 at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angul‌​ar.js:7770:28) – Prajoth Jan 23 '13 at 16:39
2  
Can you post Plunker/Fiddle that shows this error? – Valentyn Shybanov Jan 23 '13 at 18:15

Read a little in $http service provided by AngularJS

here is the hint to the code that you would use

$scope.add = function(){
    $http.get(url).then(function(response){
        queries = response.queries;
        $scope.newMessage = queries.request.totalResults;
    })
}
share|improve this answer

See http://plnkr.co/edit/nZkYsxLHLvf3SZNiJKic?p=info

Following your link, I just found one error:

var x = searchResults.split('.');

don't have function split() if i replace: x = "abc"

the result : Steve Jobs - 515,000,000 results - a.b results dsgdfg - a.b results

share|improve this answer
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – pduersteler Aug 11 '15 at 12:27
    
@pduersteler, But its not a link only answer – Rohit Gupta Aug 11 '15 at 12:29

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.