SoFunction
Updated on 2025-04-13

Solutions to conflict between template view and AngularJS

This article describes the solution to the conflict between template view and AngularJS. Share it for your reference, as follows:

question:

In the mvc view of php, we need to load the process

Pass some data to the template:

like:

Here is a controller

$data['users'] = {something from databases};
$this->load->view('home/index',$data);

Here is the corresponding view

<div ng-controller="loadData">
   <ul>
    <!--1. We need to use the following sentence when initializing-->
    <?php foreach(users as user):?>
    <li><?=$user->name?>:<?=$user->email?><li>
    <?php endforeach?>
    <!--2. But after the end We need to use this sentence passajax renew -->
    <li ng-repeat="user in users">{{}}:{{}}</li>
  </ul>
</div>

So now the question is how to deal with the contradiction between 1 and 2?

The first solution:

<script>
 var usersPrefetch = [
  <?php foreach(users as user):?>
  {"name": "<?=$user->name?>", "email": "<?=$user->email?>"},
  <?php endforeach?>
 ];
</script>

We store the data transmitted from php in a variable and then pass
$scope assigns it, ok

The second solution (recommended):

We use the ng-if attribute to solve our problem, calling php data when users are not defined
After the ajax pass is completed, use our data and define $

<ul ng-if="!users">
 <?php foreach(users as user):?>
 <li><?=$user->name?>:<?=$user->email?><li>
 <?php endforeach?>
</ul>
<ul ng-if="users">
 <li ng-repeat="user in users">{{}}:{{}}</li>
</ul>

demo demo address: /mser49aq/1/

I hope this article will be helpful to everyone's AngularJS programming.