2

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>

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>

Is it possible to do so? If not, how to get the data received from server, If not use $http.post/get?

4

1 回答 1

8

您可以使用ng-init将模型保存在单独的文件中:

<ul ng-init="positions = <?php echo htmlspecialchars(json_encode($Items)); ?>">
    <li ng-repeat="position in positions | filter:query">
       <p>{{position.name}}</p>
    </li>
</ul>
于 2013-07-18T15:03:47.037 回答