【发布时间】:2013-07-17 12:42:22
【问题描述】:
我最近开始学习 AngularJS。当我从数据库中获取一个数组的数据时,我使用了foreach函数,所以我的代码是
<ul>
<?php foreach($Items as $Item):?>
<li><?php echo $Item['name'];?></li>
<?php endforeach;?>
</ul>
我使用 AngularJS 是因为我需要可排序的列表元素。而我的AngularJS代码大致是
<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?
【问题讨论】:
-
我最初的感觉是这样你将无法将ng-repeat和php forEach结合起来。
-
您的代码不起作用吗?该代码对我来说看起来不错。你能显示 json_encode 的 JSON 输出吗?
-
我认为最好不要在同一个文件中混合 PHP 和 JS 代码。为什么不使用 AJAX 来检索 Items 数组而不是使用 PHP 来回显它?
-
MaxPRafferty,此代码有效,但我不确定我在做什么正确,因为文档说模型应该在一个单独的文件中,那么也许我会发送数据通过 ajax
标签: php javascript angularjs angularjs-ng-repeat