AngularJS 和 php 数组数据 [英] AngularJS and php array data

查看:19
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆