Issue
I'm fetching data from the database using Laravel 4.2, it returns an array of JSON objects, then I pass that data to a view. But I'm unable to show it using Angular JS' ng-repeat directive. How can I do taht? Note for Blade I'm using [[ ]], and for Angular JS I'm using {{ }}. Controller of Laravel is given as Following:
class CountryController extends BaseController {
public function index()
{
return View::make('admin.country.show')
->with('countries', Country::all());// returns array of JASON objects
}
}
View of Laravel is given as follows:
<table class="table table-bordered">
<tr>
<th class="col-lg-2">Sr.No</th><th>Country</th>
</tr>
<!-- foreach($countries as $country) -->
<tr ng-repeat="country in $countries">
<td class="col-lg-2">{{country.id}}</td><td>{{country.country}}</td>
</tr>
<tr><td>{{query}}</td></tr>
<!--endforeach -->
</table>
{{countries}}
[[ $countries]]
{{countries}} of ajs not works but [[$countries]] works! How can I access $countries in AJS?
Solution
I don'n know that whether it's a good practice or not but I've found the solution which is given as follows:
<table class="table table-bordered">
<tr>
<th class="col-lg-2">Sr.No</th><th>Country</th>
</tr>
<tr ng-repeat="country in $countries">
<td class="col-lg-2">{{country.id}}</td><td>{{country.country}}</td>
</tr>
</table>
Instead of using
<tr ng-repeat="country in $countries">
I've used the
<?php echo("<tr ng-repeat='country in ". $countries. "'>"); ?>
It, after rendering becomes
<tr class="ng-scope" ng-repeat="country in [{"id":1,"countries":"Pakistan"},{"id":2,"countries":"Saudi Arbia"}]">
You can clearly see that an array of JSON object is returned by php, which now can easly be printed. Note again: I don't know that it's best practice or not! I still would like to know the best practice.
Answered By - MKJ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.