简体   繁体   中英

Getting Json Data From API using Angular Js

I am Trying to get Data from Dhis2 Api Using AngularJs . .But Data is not displaying. Im simply getting data form the api and diplaying the name and the id on the page. but i am not getting any error but the data is not displayed I don't know where the problem is.

Here is my angular js file in which i am trying to get data from the api using http.get.

 angular.module('myApp', []).controller('userCtrl', function($scope,$http) {

init();
function init(){
        var apiUrl = "http://localhost:8080/api/organisationUnits.json";

        // Cross-site redirect error solution: Run chrome with --disable-web-security
        var base64 = "YWRtaW46ZGlzdHJpY3Q=";
    //$http.get(apiUrl, {headers: {'Authorization': 'Basic YWRtaW46ZGlzdHJpY3Q='}}).            
    $http.get(apiUrl, {headers: {'Authorization': 'Basic YWRtaW46ZGlzdHJpY3Q='}}).
        success(function(data) {

    // $scope.users = [];
         $scope.users = data;
         alert('success');
        }).
        error(function(data, status, headers, config) {
            alert("Error. Data: " + data);
        });
    }

Here is my HTML page. on which i am trying to display name and the id which i got from the api .

    <body ng-app="myApp" ng-controller="userCtrl">

<div class="container">

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">DHIS2</a>
    </div>
    <div>
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <!--<li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li> 
        <li><a href="#">Page 3</a></li> !-->
      </ul>
    </div>
  </div>
</nav>

<h4>Search For the Organization Units</h4>

<div class="row">


        <div class="col-sm-3 col-md-6" >
         <input type="text" class="form-control" ng-model="filters.search" placeholder="Search for Organization Units">
        </div>
        <div class="col-sm-9 col-md-6" >

       <label class="custom-select" >    
      <select  ng-model="filters.searchdd">
      <option selected disabled value="" style="display:block;"> Plese Select Level</option>
      <option  value="private">private</option>
      <option value="simple">simple</option>
      <option value="vip">vip</option>
      </select>  
           </label> 





    </div>

  </div>


<table class="table table-striped">
  <thead><tr>
    <th>Edit</th>
    <th>Organizational Units</th>
    <th>Levels</th>
      <th>Drop Down Menu
    </th>
  </tr></thead>
  <tbody><tr ng-repeat="user in users | filter:filters.search| filter: filters.searchdd">
    <td>
      <button class="btn" ng-click="editUser(user.id)" >
      <span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;Edit
      </button>
    </td>
    <td>{{ user.name }}</td>
    <td>{{ user.id }}</td>
      <td><label class="custom-select" >    
      <select  ng-model="filters.searchdd">
      <option selected disabled value="" style="display:block;"> Search or Edit</option>
      <option  value="1">Locate on Map</option>
      <option value="2">Edit Coordinates</option>
      <option value="3">Edit org. unit details</option>
           <option value="4">Add new facility</option>
      </select>  
           </label> </td>
  </tr></tbody>
</table>

you pipe your data to two filters. Are you sure your data would pass them? And of course it seems there is a missing closing bracket, but maybe you just missed it while copying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM