mardi 15 mars 2016

Angularjs : how to show contents of ng-repeat with Laravel?

I've been trying to show the contents of ng-repeat. I use Laravel 5.2, which apparently makes this task more complicated.

In case you haven't tried this, let me explain that using double curly braces (e.g. {{ value }}) is going to find a php variable ($value), instead of properties in Angular controllers, if you use Laravel.

This can be solved by preceding with @, according to the Laravel's documentation. Also, I learned that there are some other ways to show variables other than {{ value }}. Still, I can't show values of ng-repeat on HTML.

My code looks like this.

<div ng-app="angularApp" ng-controller="TableController as tc">
<table>
    <tr ng-repeat="s in tc.data.students">
        <td>@{{ s.name }}</td> //1st attempt
        <td>@{!! s.name !!}</td>     //2nd attempt
        <td ng-bind="s.name"></td>        //3rd attempt
    </tr>
</table>

<script type="text/javascript">
    angular.module('angularApp', [])
    .controller('TableController', function(){
        this.data = {
            students: [
            @foreach($students as $student)
            "{!! $student !!}", //This can be replaced with {{ $student }}
            @endforeach
            ]
        };
    });
</script>

$students is an array, containing SQL objects called student. This comes from a Laravel's controller function.

Do you see anything I'm doing wrong? Any advice will be appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire