Angular JS: $http.get and $routerParams

Asked

Viewed 190 times

0

How to use routeParams in $http.get to return only 1 object? I want to bring only the object that has the same ID passed in the nightParams.

<a href="funcionario/edit/{{funcionario.id}}" class="btn btn-primary btn-xs" ng-click="">
     Editar
</button>

By clicking edit is directed to the page with the correct id. For example: localhost/funcio/Edit/98. Use the routes below.

var app = angular.module('funci',
                         ['ngRoute','ui.bootstrap','angular-confirm','ngSanitize' ]);

app.config(function($routeProvider, $locationProvider){

  $locationProvider.html5Mode(true);

  $routeProvider.when('/funcionarios', {
    templateUrl: 'partials/funcionarios.html',
    controller: 'FuncionariosController'
  });

  $routeProvider.when('/funcionario/novo', {
    templateUrl: 'partials/funcionario.html',
    controller: 'FuncionarioController'
  });

  $routeProvider.when('/funcionario/edit/:id', {
    templateUrl: 'partials/funcionario.html',
    controller: 'FuncionarioController'
  });

  $routeProvider.otherwise({
    redirectTo: '/funcionarios'
  });

});

On this page, I use a different controller, called: funcioController. It’s in this controller that I’m trying to give a console.log only on the id object passed on routeParams, but I can’t get it.

app.controller('FuncionarioController', function($scope, $http, $routeParams){
  $scope.funcionario = {};

  $http.get('http://localhost/project-funci/api/getData.php'  + $routeParams.id)
  .then(function(funcionario) {
    $scope.funcionario = funcionario.data;
    console.log($scope.funcionario);
  });

getdata.php:

<?php
include "connect.php";

$query = "select * from funcionario";

      if ($result = mysqli_query($dbc, $query))
         {
          while($row = $result->fetch_object())
          {
              foreach($row as $key => $col){
                 $col_array[$key] = utf8_encode($col);
              }
              $row_array[] =  $col_array;
          }
         echo json_encode($row_array, JSON_NUMERIC_CHECK);
      }
      mysqli_close($dbc);

?>

Result of getData.php

[{"id":98,"cod_equipe":1,"cod_cargo":1,"name":"Fulano","matricula":7414}

Currently, the console.log returns printed the entire html of the page.

Please, could someone help me?

1 answer

0

You are not passing the parameter in the GET URL or receiving the value passed in the php select script.

An example passing the parameter by GET would look like this:

('http://localhost/project-funci/api/getData.php?id='+ $routeParams.id)

After that, you can receive it in PHP through the global variable:

$getId = $_GET["id"];

And insert it into the sql query.

$query = "select * from funcionario WHERE id = $getId";

Remember to validate the field before sending and also validate in PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.