我使用Laravel 4.2从数据库中获取数据,它返回JSON对象数组,然后将数据传递给视图。但是我无法使用Angular JS的ng-repeat指令显示它。我该怎么办?请注意,对于Blade我正在使用[[]],对于Angular JS我正在使用{{}}。Laravel的控制器如下所示:
class CountryController extends BaseController {
public function index()
{
return View::make('admin.country.show')
->with('countries', Country::all());// returns array of JASON objects
}
}
拉拉夫勒的景色如下:
<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]]
ajs的{{countries}}个无效,但[[$ countries]]个有效!如何在AJS中访问$ countries?
我不知道这是否是一个好习惯,但是我找到了如下解决方案:
<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>
而不是使用
<tr ng-repeat="country in $countries">
我用过
<?php echo("<tr ng-repeat='country in ". $countries. "'>"); ?>
渲染之后
<tr class="ng-scope" ng-repeat="country in [{"id":1,"countries":"Pakistan"},{"id":2,"countries":"Saudi Arbia"}]">
您可以清楚地看到php返回了一个JSON对象数组,现在可以轻松地打印该数组了。再次注意:我不知道这是否是最佳实践!我仍然想知道最佳做法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句