Angularjs learning 4 (solving conflicts between the template view and angularjs), template angularjs

Source: Internet
Author: User
Tags php foreach

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/
















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.