7

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!

1
  • plnkr.co/edit/nZkYsxLHLvf3SZNiJKic Commented Jan 24, 2013 at 5:10

3 Answers 3

15

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);
  });
};
4
  • 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? Commented Jan 22, 2013 at 20:44
  • 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 :( Commented Jan 23, 2013 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/angular.js:6129:19 at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js:12496:13 at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js:7770:28) Commented Jan 23, 2013 at 16:39
  • at Object.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js:7850:23) at <error: TypeError: Accessing selectionDirection on an input element that cannot have a selection.> at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js:1930:10 at Array.forEach (native) at forEach (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js:110:11) at <error: TypeError: Accessing selectionDirection on an input element that cannot have a selection.> Commented Jan 23, 2013 at 16:44
1

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

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

2
  • 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. Commented Aug 11, 2015 at 12:27
  • @pduersteler, But its not a link only answer Commented Aug 11, 2015 at 12:29

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.