0

Edit: James P. led me to determine that the issue appears to be with CORS and not necessarily with any of the Angular code. Please read comments below.

I am very new to AngularJS and JS altogether, so I'm sure the answer to this is something simple that I have overlooked so thank you in advance.

I am using Angular Seed and I have created an API that is verified working (as in, I go to my URL:3000/getstuff and it displays queries from my mongodb just fine).

That API returns a JSON format response from a mongodb with 3 key/pairs including id. I am able to view it in browser just fine.

So in Angular seed, very basic view1/view1.js I modified to as such:

'use strict';

angular.module('myApp.view1', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1', {
    templateUrl: 'view1/view1.html',
    controller: 'View1Ctrl'
  });
}])

.controller('View1Ctrl', ['$scope', '$http', function($scope, $http) {
  //$scope.test = "test";
  //the above works when I bind in view1.html

  $http.get('http://x.x.x.x:3000/getstuff').
  success(function(response) {
    $scope.information = response;
  });

}]);

And it is not working as I thought it might. So when I try to bind this response in view1.html with a simple {{information}} it's blank. This code did not break the app either, it still works and I am able to display {{test}} if I uncomment it.

Anyhow, any help would be very much appreciated. And for the record, I have been reading up on this for days and days before posting this. I am just a novice is all.

Tom V
  • 15
  • 5

1 Answers1

0

You need to use angular.copy to copy the data to your $scope variable:

  $http.get('http://x.x.x.x:3000/getstuff')
            .then(function (response) {
                // Success
                angular.copy(response.data, $scope.information);
            }, function () {
                // Failure
        });

Then in your view:

<div ng-controller="MainController">
   <div ng-repeat="item in information">
      <p>{{item.id}}</p>
      <p>{{item.type}}</p>
      <p>{{item.text}}</p>
    </div>
</div>

Where id ,type and text are properties of each item in the Array.

EDIT: Just for completeness the following is a working example, with above html snippet.

(function () {

"use strict";

angular.module("app", []);

angular.module("app").controller("MainController", mainController);

function mainController($scope, $http) {

    $scope.information = [];

    $http.get("http://api.scb.se/OV0104/v1/doris/en/ssd")
          .then(function (response) {
              // Success
              angular.copy(response.data, $scope.information);
          }, function () {
              // Failure
          });

}

})();
James P
  • 2,201
  • 2
  • 20
  • 30
  • Hello James P. Thanks a lot for trying to help me out. I did add the angular.copy and same results, nothing is displaying. In my view1.html should I try {{response.data.[key name]}} or just {{response}} or {{response.[key name]}}? I have tried all ways, nothing worked yet. But with your angular.copy suggestion it still isn't working, nothing is displaying from the API call. Edit: I mean information.[key] – Tom V Sep 17 '16 at 21:07
  • So I have plane Jane this: Collected at: {{information.datestamp}} (one of my json key/pairs is a timestamp). My Express log IS showing that every time I reload the page the API is being hit. So that narrows it down to something with the way I'm handling the data. Is my problem that I am trying to store a 3 key/pair json call return as one variable? Should it be an array instead? $scope.information {} ? again.. please excuse my ignorance. – Tom V Sep 17 '16 at 21:16
  • Yes it should be an array the you can just use `ng-repeat` to loop through the array - e.g. see above, will add to answer. – James P Sep 17 '16 at 21:18
  • Dang. Still getting all blanks on the view1.html page. I am using:

    {{item.id}}

    {{item.datestamp}}

    {{item.top100}}

    and those are all of my keypairs. I also tried ng-repeat with
  • – Tom V Sep 17 '16 at 21:29
  • Make sure you declare `$scope.information = [];` before the `$http.get`... - also are you getting any errors on console in F12 dev tools. – James P Sep 17 '16 at 21:37
  • Just tried it with $scope.information = []; and no dice. I don't know what F12 dev tools are? I am using Webstorm in windows and uploading to Github and then pulling onto my linux box on AWS EC2. – Tom V Sep 17 '16 at 21:40
  • What browser are you using to display View1.html. In browser, e.g. Chrome hit F12 - and select the console tab. Copy/paste any errors. – James P Sep 17 '16 at 21:42
  • Aaaah good call. XMLHttpRequest cannot load http://x.x.x.x:3000/top100. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://x.x.x.x:8080' is therefore not allowed access. where x.x.x.x is my web server on AWS... I have 3000 and 8080 set as a security rule to allow. I can see the API if I load it directly as page, it returns the appropriate JSON results from the mongodb. Weird. What could this mean? Just googled it -- this is a whole new can of worms. Yikes. – Tom V Sep 17 '16 at 21:45
  • I found http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource -- It's odd, because like I've said I can view the API by directly linking to it.... – Tom V Sep 17 '16 at 21:51