title: Passing Parameter to Angular
body: <p>I am going to create a controller that will list distinct a selected field in a selected table in database and pass it to my API.</p>
  
  <p>Currently, i am using a dirty method which is create several controller that has the field name and table name in it.</p>
  
  <p><strong>controller.js</strong></p>
  
  <pre><code>.controller('ListDistinctCustomerCtrl', function($scope, $http) {
    var xhr = $http({
      method: 'post',
      url: 'http://localhost/api/list-distinct.php?table=customer&amp;field=cust_code'
    });
    xhr.success(function(data){
      $scope.data = data.data;
    });
  })
  
  .controller('ListDistinctSupplierCtrl', function($scope, $http) {
    var xhr = $http({
      method: 'post',
      url: 'http://localhost/api/list-distinct.php?table=supplier&amp;field=supp_code'
    });
    xhr.success(function(data){
      $scope.data = data.data;
    });
  })
  </code></pre>
  
  <p>and this is the API file</p>
  
  <p><strong>list-distinct.php</strong></p>
  
  <pre><code>&lt;?php
  require_once '/config/dbconfig.php';
  $table = $_GET['table'];
  $field = $_GET['field'];
  GetData($table,$field);
  
  function GetData($tablename,$fieldname) {
      $sql = "SELECT distinct $fieldname as expr1 FROM $tablename order by expr1 asc";
      try {
          $db = getdb();
          $stmt = $db-&gt;prepare($sql);
          $stmt-&gt;execute();
          $data = $stmt-&gt;fetchAll(PDO::FETCH_OBJ);
          $db = null;
          echo json_encode(array('data' =&gt; $data));
      } catch(PDOException $e) {
          echo '{"error":{"text":'. $e-&gt;getMessage() .'}}';
      }
  }
  ?&gt;
  </code></pre>
  
  <p>I believe there is a clean and better way to do this.</p>