2016-11-30 7 views
1

私はAngularJSの初心者です。私はflexibile関数を書いて、selectをデータベーステーブルの内容で埋めたいと思っています。私はすでに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ありがとうございます。 –

関連する問題