Angularjs learning 4 (solving conflicts between the template view and angularjs), template angularjs
Problem:
In the mvc view of php, we need
Pass some data to the template:
For example:
// Here is a controller $ data ['users'] = {something from databases}; $ this-> load-> view ('home/Index', $ data );
// The View <div ng-controller = "loadData"> <ul> <! -- 1. We need to use the following sentence during initialization --> <? Php foreach (users as user):?> <Li> <? = $ User-> name?> : <? = $ User-> email?> <Li> <? Php endforeach?> <! -- 2. however, we need to use this sentence to update via ajax --> <li ng-repeat = "user in users" >{{ user. name }}:{ {user. email }}</li> </ul> </div>
Now, how can we deal with the conflict between 1 and 2?
Solution 1:
<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 the variable, and then use
$ Scope assigns a value to it, OK
Solution 2 (recommended ):
We use the ng-if attribute to solve our problem. php data is called when users is undefined.
After ajax is passed, use our data and define $ scope. users
<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">{{user.name}}:{{user.email}}</li></ul>
Demo: https://jsfiddle.net/mser49aq/1/