0

I am trying to search JSON with Angular. I have found several tutorials around the web for how to do this, but I am now stuck and having trouble knowing what my problem is. I have tried using the debugger in Chrome and it seems to go through the JS completely, but nothing is displayed.

I am a beginner, so forgive me if I have some obvious mistakes.

Here is my code

HTML:

<!DOCTYPE html>
<!doctype html>
<html ng-app="spellSearch">
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="spellSearch.js"></script>
  </head>
  <body>
    <div ng-controller="spellSearchCtrl">
      <label>Name:</label>
      <input type="text" ng-model="searchString" placeholder="Enter any spell information">
        <ul ng-repeat="i in jsonSpellData | filter:searchString">
          <li>
          Name: {{i.name}} <br>
          Description: {{i.desc}} <br>
          Page Number: {{i.page}} <br>
          Range: {{i.range}} <br>
          Components: {{i.components}} <br>
          Material: {{i.material}} <br>
          Ritual: {{i.ritual}} <br>
          Duration: {{i.duration}} <br>
          Concentration: {{i.concentration}} <br>
          Casting Time: {{i.casting_time}} <br>
          Level: {{i.level}} <br>
          School: {{i.school}} <br>
          Class: {{i.class}}
        </li>
        </ul>
    </div>
  </body>
</html>

JavaScript

var app = angular.module('spellSearch', []);

app.controller('spellSearchCtrl', function($scope, $http){
    $http.get('spells.json').success(function(data, status, headers, config){
        $scope.items = data.data;
    }).error(function(data, status, headers, config){
        console.log("No data found...");
    });
});

app.filter('searchFor', function(){
    return function(arr, searchString){
        if(!searchString){
            return arr;
        }
        var result = [];
        searchString = searchString.toLowerCase();
        angular.forEach(arr, function(item){
            if(item.name.toLowerCase().indexOf(searchString) !== -1){
                result.push(item);
            }
            else {
                result.push("No results.");
            }
        });
        return result;
    };
});

Part of the JSON

var jsonSpellData = [
  {
    "name":"Abi-Dalzim's Horrid Wilting",
    "desc":"<p>You draw the moisture from every creature in a 30-foot cube centered on a point you choose within range. Each creature in that area must make a Constitution saving throw. Constructs and undead aren't affected, and plants and water elementals make this saving throw with disadvantage. A creature takes 10d8 necrotic damage on a failed save, or half as much damage on a successful one.You hurl a bubble of acid. Choose one creature within range, or choose two creatures within range that are within 5 feet of each other. A target must succeed on a Dexterity saving throw or take 1d6 acid damage.</p><p>This spells damage increases by 1d6 when you reach 5th Level (2d6), 11th level (3d6) and 17th level (4d6).</p>",
    "page":"ee pc 15",
    "range":"150 feet",
    "components":"V, S, M",
    "material":"A bit of sponge.",
    "ritual":"no",
    "duration":"Instantaneous",
    "concentration":"no",
    "casting_time":"1 action",
    "level":"8th-level",
    "school":"Necromancy",
    "class":"Sorcerer, Wizard"
  },
  {
    "name":"Absorb Elements",
    "desc":"<p>The spell captures some of the incoming energy, lessening its effect on you and storing it for your next melee attack. You have resistance to the triggering damage type until the start of your next turn. Also, the first time you hit with a melee attack on your next turn, the target takes an extra 1d6 damage of the triggering type, and the spell ends.</p>",
    "higher_level":"<p>When you cast this spell using a spell slot of 2nd level or higher, the extra damage increases by 1d6 for each slot level above 1st.</p>",
    "page":"ee pc 15",
    "range":"Self",
    "components":"S",
    "ritual":"no",
    "duration":"1 round",
    "concentration":"no",
    "casting_time":"1 action",
    "level":"1st-level",
    "school":"Abjuration",
    "class":"Druid, Ranger, Wizard"
  }
];
2

2 Answers 2

1

You can filter data directly inside ng-repeat. Just remove your custom filter altogether.

You can also add an ng-if="searchString" so that only filtered spells appear.

  <input type="text" ng-model="searchString" placeholder="Enter any spell information">
    <ul ng-if="searchString" ng-repeat="i in spell.jsonSpellData | filter:searchString">
      <li>
        Name: {{i.name}}
      </li>
    </ul>

function spellSearchCtrl() {
  this.jsonSpellData = [ { "name":"Abi-Dalzim's Horrid Wilting" }, { "name":"Absorb Elements" } ];
}
angular.module('myApp', []);
angular
    .module('myApp')
    .controller('spellSearchCtrl', spellSearchCtrl);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="spellSearchCtrl as spell">
      <label>Name:</label>
      <input type="text" ng-model="searchString" placeholder="Enter any spell information">
        <ul ng-if="searchString" ng-repeat="i in spell.jsonSpellData | filter:searchString">
          <li>
            Name: {{i.name}}
          </li>
        </ul>
    </div>
</div>

1

Anytime validate your json with
https://jsonformatter.curiousconcept.com/
Remove assignment and semi colon from your JSON file.

[
  {
    "name":"Abi-Dalzim's Horrid Wilting",
    "desc":"<p>You draw the moisture from every creature in a 30-foot cube centered on a point you choose within range. Each creature in that area must make a Constitution saving throw. Constructs and undead aren't affected, and plants and water elementals make this saving throw with disadvantage. A creature takes 10d8 necrotic damage on a failed save, or half as much damage on a successful one.You hurl a bubble of acid. Choose one creature within range, or choose two creatures within range that are within 5 feet of each other. A target must succeed on a Dexterity saving throw or take 1d6 acid damage.</p><p>This spells damage increases by 1d6 when you reach 5th Level (2d6), 11th level (3d6) and 17th level (4d6).</p>",
    "page":"ee pc 15",
    "range":"150 feet",
    "components":"V, S, M",
    "material":"A bit of sponge.",
    "ritual":"no",
    "duration":"Instantaneous",
    "concentration":"no",
    "casting_time":"1 action",
    "level":"8th-level",
    "school":"Necromancy",
    "class":"Sorcerer, Wizard"
  },
  {
    "name":"Absorb Elements",
    "desc":"<p>The spell captures some of the incoming energy, lessening its effect on you and storing it for your next melee attack. You have resistance to the triggering damage type until the start of your next turn. Also, the first time you hit with a melee attack on your next turn, the target takes an extra 1d6 damage of the triggering type, and the spell ends.</p>",
    "higher_level":"<p>When you cast this spell using a spell slot of 2nd level or higher, the extra damage increases by 1d6 for each slot level above 1st.</p>",
    "page":"ee pc 15",
    "range":"Self",
    "components":"S",
    "ritual":"no",
    "duration":"1 round",
    "concentration":"no",
    "casting_time":"1 action",
    "level":"1st-level",
    "school":"Abjuration",
    "class":"Druid, Ranger, Wizard"
  }
]

HTML File

<html ng-app="spellSearch">
  <head>
    <meta charset="utf-8">
    <title>Angular.js JSON Fetching Example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script>
    var app = angular.module('spellSearch', []);
      app.controller('spellSearchCtrl', function ($scope, $http){
        $http.get('spells.json').success(function(data) {
          $scope.spells = data;
        });
      });  
    </script>
  </head>
  <body ng-controller="spellSearchCtrl">
    <h2>Angular.js JSON Fetching Example</h2>
    <div>
      <label>Name:</label>
      <input type="text" ng-model="searchString" placeholder="Enter any spell information">
        <ul ng-repeat="i in spells | filter:searchString">
          <li>
          Name: {{i.name}} <br>
          Description: {{i.desc}} <br>
          Page Number: {{i.page}} <br>
          Range: {{i.range}} <br>
          Components: {{i.components}} <br>
          Material: {{i.material}} <br>
          Ritual: {{i.ritual}} <br>
          Duration: {{i.duration}} <br>
          Concentration: {{i.concentration}} <br>
          Casting Time: {{i.casting_time}} <br>
          Level: {{i.level}} <br>
          School: {{i.school}} <br>
          Class: {{i.class}}
        </li>
        </ul>
    </div>
  </body>
</html>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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