AngularJS and php array data

2019-05-25 19:17发布

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?

1条回答
可以哭但决不认输i
2楼-- · 2019-05-25 20:05

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>
查看更多
登录 后发表回答