AngularJS和PHP数组数据 [英] AngularJS and php array data
本文介绍了AngularJS和PHP数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最近开始学习AngularJS.当我从数据库的数组中获取数据时,我使用了foreach函数,所以我的代码是
I recently started learning AngularJS. When I getting data from the database an array, I using function foreach, so my code is
<ul>
<?php foreach($Items as $Item):?>
<li><?php echo $Item['name'];?></li>
<?php endforeach;?>
</ul>
我使用AngularJS是因为我需要可排序的列表元素.而我使用AngularJS编写的代码大致是
I use AngularJS because I need sortable list elements. And my code with AngularJS roughly the
<script>
function PositionsList($scope){
$scope.positions=<?php echo json_encode($Items);?>
}
</script>
<ul>
<li ng-repeat="position in positions | filter:query">
<p>{{position.name}}</p>
</li>
</ul>
是否可以这样做?如果不是,如何获取从服务器接收的数据,如果不使用$ http.post/get?
Is it possible to do so? If not, how to get the data received from server, If not use $http.post/get?
推荐答案
您可以使用 ng-init 将模型保存在单独的文件中:
You can use ng-init to keep your model in a separate file:
<ul ng-init="positions = <?php echo htmlspecialchars(json_encode($Items)); ?>">
<li ng-repeat="position in positions | filter:query">
<p>{{position.name}}</p>
</li>
</ul>
这篇关于AngularJS和PHP数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文