AngularJS根据另一个下拉列表中的数据填充下拉列表?

时间:2016-07-13 23:11:07

标签: php mysql angularjs

我尝试使用following作为起点,根据第一个选项中的选择动态填充第二个下拉列表。

使我有点困难的当然是我连接到MySQL数据库来获取我的数据......

这是index.php文件

<div ng-app ng-controller="BlaBla">
<select ng-model="GetStartCategories" ng-options="category.data for category in categories1" ng-change="GetCategories2()">
</select>
<select ng-model="Get2ndCategories" ng-options="category.data for category in categories2">
</select>

get2nd.php的重要部分如下所示:

//DB Connection
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
$ancestor = $_GET["ancestor"];
$sql = "select * from cat_node n
Join cat_hierarchy h
on(n.id=h.descendant)
where h.ancestor = '{$ancestor}' and depth = 1
group by descendant";

//Use prepared statements as good practice
$stmt = $dbh->prepare($sql);

 //Execute query
$stmt->execute();

//Fetch the results into an array
$result = $stmt->fetchall(PDO::FETCH_ASSOC);

//Convert to JSON
$json = json_encode($result);

//echo the JSON-string
echo($json);

使用以下&#34;伪代码&#34;我能够获取并填充第一个下拉列表:

function BlaBla($scope, $http) {
$http.get('get1st.php').
 success(function(data){
  $scope.categories1 = data;
  $scope.getCategories2 = function(){
    $http.get('get2nd.php?ancestor='+$categories1.id).success(function(data2){
      $scope.categories2 = data2;  
    });

   };
 });  
};

但第二次下拉当然没有填充=)

有什么建议吗?

哦,对于你的信息,这是Closure-table模型: Closure Table Model

0 个答案:

没有答案