Resolution of conflicts between template views and Angularjs _angularjs

Source: Internet
Author: User
Tags php foreach

The example in this article describes a workaround for the conflict between the template view and the Angularjs. Share to everyone for your reference, specific as follows:

Problem:

In the MVC view of PHP, we need to be in the process of loading

Pass some data to the template:

Such as:

This is some controller.

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

Here is the corresponding view

<div ng-controller= "LoadData" >
   <ul>
    <!--1. When initializing, we need to use the following sentence-->
    <?php foreach ( Users as user):?>
    <li><?= $user->name?>:<?= $user->email?><li>
    <?php Endforeach?>
    <!--2. But after the end we need to use this to update-->
    <li ng-repeat= "user in Users" >{{user.name} through Ajax: {{user.email}}</li>
  </ul>
</div>

So now the question is, how do you deal with the contradiction between 1 and 2?

First Solution:

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

We store the data from PHP in a variable and then pass
$scope to assign it, OK

Second solution (recommended):

We use the Ng-if property to solve our problem by invoking PHP data when the user is undefined
Use our data and define $scope when the Ajax pass is complete. 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 Demo Address: https://jsfiddle.net/mser49aq/1/

I hope this article will help you to Angularjs program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.