Angularjs Learning Four (resolving conflicts between template views and Angularjs)

Source: Internet
Author: User
Tags php foreach


Problem:
In the MVC view of PHP, we need to be in the process of loading
Pass some data to the template:
Such as:

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. 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 phrase via AJAX update--        <li ng-repeat= "user in Users" >{{ User.name}}:{{user.email}}</li>    </ul></div>

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

The first of these solutions:

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

We store the data passed by PHP in a variable and then
$scope assign a value to it, OK

Second solution (recommended):

We use the Ng-if property to solve our problem and call PHP data when users are 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 Address: https://jsfiddle.net/mser49aq/1/
















Angularjs Learning Four (resolving conflicts between template views and Angularjs)

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.