3

I am loading a bunch of data from a json file, and have used both $http.get and also Restangular to grab it.

Whenever I pass this data into the $scope I keep getting this error multiple times:

Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html

This seems to fire multiple times for each item in the array, so unsure if the data itself is bad somehow?

The problem only happens if I pass it to $scope, and according to Can we use $sce.trustAsHtml(string) out of "filter"s? its happening when you call trustAsHtml on something twice, which I am not. Infact I'm not even calling this once, so can only assume thats happening somewhere when I pass it to $scope

$http.get('/airlines.json')
.success(function(data) {
    $scope.airlines = data;
    console.log(data);
});

I can't find much else about about this error thats helpful. The data is below:

[
  {
    "id":1,
    "name":"Private flight",
    "alias":"\\N",
    "iata":"-",
    "icao":"N/A",
    "callsign":"",
    "country":"",
    "active":"Y"
  },
  {
    "id":2,
    "name":"135 Airways",
    "alias":"\\N",
    "iata":"",
    "icao":"GNL",
    "callsign":"GENERAL",
    "country":"United States",
    "active":"N"
  },
  {
    "id":3,
    "name":"1Time Airline",
    "alias":"\\N",
    "iata":"1T",
    "icao":"RNX",
    "callsign":"NEXTIME",
    "country":"South Africa",
    "active":"Y"
  }
]

Question: Why is this error coming up when I pass it to the scope, and how can I get rid of it?

Community
  • 1
  • 1
Horse
  • 3,023
  • 5
  • 38
  • 65
  • And where are you using $sce? – Shomz Jan 30 '15 at 11:37
  • I'm not, unless its something funky happening in $scope based on using appgyver supersonic with angular? – Horse Jan 30 '15 at 12:10
  • Something has to be loading it and attaching it to that scope variable. – Shomz Jan 30 '15 at 12:12
  • Yup, it definitely isn't in my controller though. Interestingly it only happens if I use a multidimensional json object, if I just do a single level of values it works fine. – Horse Feb 02 '15 at 12:50
  • It happened to me when one of my property values were actually an array instead of string. in your case it might be happening because of speacial characters like \\ and / – gaurav5430 May 11 '16 at 13:58

1 Answers1

-1

the solution lies in the way you are handling this JSON object.

 $scope.airlines = data; 

is not the correct way as your response is a array of JSON objects and not just one JSON object . [ ] enclosure in JSON is used as an array so

 $scope.airlines = data[0]; 

will handel the first JSON , this is just an crude not clean method, but you can add the array elements via a loop.

A JSON response without array [] can be handelled by angularJS by the first method just fine but with an array needs the second.

hope that helps.

anil keshav
  • 479
  • 1
  • 5
  • 18