我正在使用AngularJS制作即时搜索表单,此时列表是静态的,但是我想让它变为动态,以便从数据库中提取的结果被发送到JS脚本并以适当的格式进行清理这样就可以动态生成结果。
AngularJS:
angular.module('sortApp', [])
.controller('mainController', function($scope) {
$scope.sortType = 'name'; // set the default sort type
$scope.sortReverse = false; // set the default sort order
$scope.searchFish = ''; // set the default search/filter term
// create the list of people
$scope.names = [
{ name: '' },
];
});
我的PHP和HTML:
$sql = "SELECT id, first_name, last_name from library";
$results = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($results)) {
$id=$row["id"];
$first_name=$row["first_name"];
$last_name=$row["last_name"];
echo htmlentities(json_encode($first_name->names));
}
?>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<td>
<a href="#" ng-click="sortType = 'name'; sortReverse = !sortReverse">
Name
<span ng-show="sortType == 'name' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'name' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'team'; sortReverse = !sortReverse">
Team
<span ng-show="sortType == 'team' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'team' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="schedular in names | orderBy:sortType:sortReverse | filter:searchname">
<td>{{ schedular.name }}</td>
<td>{{ schedular.team }}</td>
</tr>
</tbody>
</table>
因此,只要打开页面,就会根据我的SQL代码循环查询,需要将SQL查询的结果发送到AngularJS表单并填入:
$scope.names = [
{ name: '' },
];
});
静态地说这是AngularJS的样子:
angular.module('sortApp', [])
.controller('mainController', function($scope) {
$scope.sortType = 'name'; // set the default sort type
$scope.sortReverse = false; // set the default sort order
$scope.searchFish = ''; // set the default search/filter term
// create the list of people
$scope.names = [
{ name: 'John Smith' },
];
});
但我希望动态拉动“John Smith”这个名字并发送给AngularJS。如何将动态生成的PHP变量发送到AngularJS并确保它们已正确清理?任何帮助将不胜感激。