2016-11-30 53 views
1

我是AngularJS的新手,我想写一个flexibile函数来用数据库表内容填充select。我已经可以填写选择,但我无法为其设置第一个值。如何设置由数据库表填充的选择的第一个值?

我的代码:

HTML:

<div id="super-regional-modal" ng-controller="RegionalController"> 
    <form name="regionalform" id="regionalform"> 
     <div class="form-group"> 
      <div class="row"> 
       <div class="input-field col s12"> 
        <span style="color:#DA1022;">* </span><label for="regionalInfoTipoRegional">Label Test</label> 
        <select class="form-control" id="regionalInfoTipoRegional" name="regionalInfoTipoRegional" ng-model="regionalInfo.tipoRegional" ng-init="regionalInfo.tipoRegional = populateSelect('select * from wrk_tests')" ng-options="tiporegional as tiporegional.cidade for tiporegional in populate_select_output track by tiporegional.id" ng-required="true"></select> 
       </div> 
      </div> 
     </div> 
    </form> 
</div> 

JS:

'use strict'; 
angular.module("acsApp").controller("RegionalController", ["$scope", "$http", "$state", "$stateParams", "$timeout", "$sce", "toaster", function ($scope, $http, $state, $stateParams, $timeout, $sce, toaster){ 
$scope.populateSelect = function(sqlselect){ 
    $http.post("modulos/sistema/php/sys/fillDB.php", {"sqlselect" : sqlselect} 
    ).success(function(data, status, headers, config){ 
     $scope.populate_select_output = data.records; 
    }).error(function(data, status, headers, config){ 
     console.error(data); 
    }); 
} 

PHP:

<?php 
    header("Access-Control-Allow-Origin: *"); 
    include($_SERVER["DOCUMENT_ROOT"]."/".explode("/",$_SERVER["PHP_SELF"])[1]."/functions.php"); 
    include($_SERVER["DOCUMENT_ROOT"]."/".explode("/",$_SERVER["PHP_SELF"])[1]."/clsConnection.php"); 
    $cls_database = new Database(); 
    $db    = $cls_database->getConnection(); 
    $data   = json_decode(file_get_contents("php://input")); 
    $sqlselect  = $data->sqlselect; 
    $stmt   = $db->prepare($sqlselect); 
    $stmt->execute(); 
    $num   = $stmt->rowCount(); 
    $arr   = array(); 
    if($num>0){ 
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
      extract($row); 
      $arr[] = $row; 
     } 
    } 
    header("Content-Type: application/json; charset=UTF-8"); 
    echo json_encode(['records' => $arr]); 
?> 

任何帮助表示赞赏。对不起,我英文很差。提前致谢。

回答

1

首先你需要在你的控制器定义为regionalInfo起始值:$scope.regionalInfo = {};然后分配任何的检索记录regionalInfo.tipoRegional

'use strict'; 
angular.module("acsApp").controller("RegionalController", ["$scope", "$http", "$state", "$stateParams", "$timeout", "$sce", "toaster", function ($scope, $http, $state, $stateParams, $timeout, $sce, toaster){ 
$scope.regionalInfo = {}; 
$scope.populateSelect = function(sqlselect){ 
$http.post("modulos/sistema/php/sys/fillDB.php", {"sqlselect" : sqlselect} 
).success(function(data, status, headers, config){ 
    $scope.populate_select_output = data.records; 
    $scope.regionalInfo.tipoRegional = $scope.populate_select_output[0]; // or 1, 2, etc... 
    }).error(function(data, status, headers, config){ 
    console.error(data); 
    }); 
} 

ng-init可改为:

ng-init="populateSelect('select * from wrk_tests')" 
+0

工程就像一个魅力! 谢谢mr_sudaca。 –

相关问题