How to perform a GET by ID and by Name in AngularJS?

0

I'm pretty new working with angular and node js. My problem is that I have a GET to search by ID (it works correctly), but I would like that depending on a radio bottom it will also search for me by name. Then I just get him to look for one of the two.

Deputy code:

Index.js

app.get(baseAPI + "/contacts/:name", (request, response) => {
console.log("GET /contacts/"+name);
var name = request.params.name;
contacts.get(name,(err,contacts)=>{
    if (contacts.length === 0) {
        response.sendStatus(404);
    }
    else {
        response.send(contacts);  
    }
   });
   });

app.get(baseAPI + "/contacts/:ID", (request, response) => {
console.log("GET /contacts/"+ID);
var ID = request.params.ID;
contacts.get(ID,(err,contacts)=>{
    if (contacts.length === 0) {
        response.sendStatus(404);
    }
    else {
        response.send(contacts);  
    }
});
});

another.js

$scope.getcon = function (radio, yolo){       

  if (radio === yolo) {
       $http.get("/api/v1/contacts/"+radio).then(function(response) {
 $scope.contacts= response.data;
 console.log("seleccionado");
    });
 } else {

  $http.get("/api/v1/contacts/"+yolo).then(function(response) {
    $scope.contacts= response.data;
  });
  console.log("no esta seleccionado");  
  console.log(radio);
  console.log(yolo);
}

Contacts.js

Contacts.prototype.get = function(name, callback) {
  return db.find({name:name}, callback);
};

Contacts.prototype.get = function(ID, callback) {
  return db.find({ID:ID}, callback);
};

HTML

<!Doctype html>
<html ng-app="ContactListApp">
<head>
    <title>
        Contact List
    </title>
    <link rel="stylesheet" type="text/css" href="bower_components/vendors/normalize.css">
    <link rel="stylesheet" type="text/css" href="bower_components/vendors/grid.css">
     <link rel="stylesheet" type="text/css" href="/style.css"> 
    <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css" type="text/css"> 
    <script type="text/javascript" src="/bower_components/angular/angular.min.js"></script>
    <script type="text/javascript" src="contact-list-app.js"></script>
    <script type="text/javascript" src="list-ctrl.js"></script>



</head>


<body ng-controller="ListCtrl">
    <title>Untitled</title> 


     <header>
    <div class="row test">
        <div class= "col-lg-6">
          <!--  <a href="#"><img  class="logo" src="https://www.informatica.us.es/docs/imagen-etsii/logo-ETSII-US-Vertical-Negro.png"></img></a> -->
           <a href="#"><img  class="logo" src="http://www.libreriaserviciomedico.com/files/14919"></img></a> 
         </div>

         <div class= "col-lg-6">
             <nav style="float:right">
                 <a  href="login">Sign Out</a> |  
                 <a  href="login">Help</a> 
             </nav>


         </div>
    </div>


</header>
<Section class="form1">

    <h2>Research Groups</h2> 
    <br>
    <form name="Research Groups" class="formulario">

    <table class="table table-bordered table-striped" id=tabla>
        <thead>
            <tr>
            <td><label for="search">Search by:</label></td>
            <td><input type="radio" name="gender" value="1"  id="radio1"  checked>Group ID<br>
                <input type="radio" name="luis" value="2"  id="radio2" >Group Name<br></td>


            <td><input class="form-control" ng-model="newContact.name"> </td>
            <td><button class="btn btn-primary"   ng-click="getcon(newContact.name, newContact.ID)" id="boton" >Get One </button> 

        </tr>

            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Responsable</th>
                <th>E-mail</th>

            </tr>
        </thead>

        <tbody>


        <tr ng-repeat="contact in contacts | filter:newContact.ID">
            <td> {{contact.ID}}</td>
            <td>{{contact.name}}</td>
            <td>{{contact.phone}}</td>
            <td>{{contact.email}}<button class="boton" ng-click="console.log($index)">View Details</button></td>

        </tr>
        </tbody>
    </table>
    </form>

    </Section>


</body>

    
asked by Blackout01 26.03.2018 в 15:46
source

0 answers