1

I have a json file with this values:

[
  {
    "id": "3",
    "name": "abacus",
    "type": "math device",

  },
  {
    "id": "4",
    "name": "beaker",
    "type": "science device",
  }
]

What I want is to display these values in my html file I'm thinking of how can I use the ng-repeat but I can't seem to find out how can I do it. Can you please help me? Any help would be appreciated. This is my html section on where i want to display the json:

<div class="form-group" >
        <label class="col-md-4 control-label">
            ID:
        </label>                                                  
        <div class="col-md-8">                                           
            <input type="text" name="regular" class="form-control" ng-model="id">
        </div>
        <label class="col-md-4 control-label">
            Name:
        </label>                                                  
        <div class="col-md-8">                                           
            <input type="text" name="regular" class="form-control" ng-model="name">
        </div>
        <label class="col-md-4 control-label">
            Type:
        </label>                                                  
        <div class="col-md-8">                                           
            <input type="text" name="regular" class="form-control" ng-model="type">
        </div>
            <br><br>
</div>  

2 Answers 2

1

Perhaps this is what you're looking for? jsfiddle.

HTML

<div class="form-group" ng-repeat="item in data">
  <label class="col-md-4 control-label">
    ID:
  </label>                                                  
  <div class="col-md-8">                                           
    <input type="text" name="regular" class="form-control" ng-model="item.id">
  </div>
  <label class="col-md-4 control-label">
    Name:
  </label>                                                  
  <div class="col-md-8">                                           
    <input type="text" name="regular" class="form-control" ng-model="item.name">
  </div>
  <label class="col-md-4 control-label">
    Type:
  </label>                                                  
  <div class="col-md-8">                                           
    <input type="text" name="regular" class="form-control" ng-model="item.type">
  </div>
  <br><br>
</div>  

Controller

  $scope.data = [
    { "id": "3", "name": "abacus", "type": "math device" },
    { "id": "4", "name": "beaker", "type": "science device" }
  ];
Sign up to request clarification or add additional context in comments.

6 Comments

@CALLODACITY Thanks this solved my question but how about if my json value is inside an array? how will I solve this?
@bleykFaust what do you mean inside an array? Isn't it already inside an array [{ "id": "3", ...}, {...}]?
@CALLODACITY, Actually, I got the values of that JSON through an API and then pass the values inside an array. Btw, can we continue this discussion on chat? I greatly appreciates your help
@CALLODACITY, When I tried to use this and exchange my code on this the only value that is displayed and repeated is the first one the second one is not being repeated
@bleykFaust did you look at the jsfiddle I linked? that's a fully working angular app. Just re-write your code to resemble that and it will work fine.
|
0

Try doing it like this (assuming json is defined on scope and contains your data)

<div class="form-group">
        <label class="col-md-4 control-label" ng-repeat-start="item in json">
            ID:
        </label>                                                  
        <div class="col-md-8">                                           
            <input type="text" name="regular" class="form-control" ng-model="item.id">
        </div>
        <label class="col-md-4 control-label">
            Name:
        </label>                                                  
        <div class="col-md-8">                                           
            <input type="text" name="regular" class="form-control" ng-model="item.name">
        </div>
        <label class="col-md-4 control-label">
            Type:
        </label>                                                  
        <div class="col-md-8">                                           
            <input type="text" name="regular" class="form-control" ng-model="item.type">
        </div>
            <br><br ng-repeat-end>
</div>  

2 Comments

how can I define that json inside a scope? @e-neko
Json is not javascript, what you want is transform the json to a javascript object, try JSON.parse(text) where text is a string variable with your json

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.