mardi 22 septembre 2015

How to return both view and object from Laravel 5 controller to Angular controller

When entering the url, http://ift.tt/1V82ECI, I would like the FloorplanController.php to return all floorplans to an angular controller and also return the view.

Here is FloorplanController@index:

public function index()
    {
        $floorplans = Floorplan::all();
        return view('floorplans.index', compact('floorplans'));
    }

Here is my Angular controller:

angular
.module('myApp', [])

.controller('FloorplanController', function($scope, $http) {

    $http.get('/floorplan').success(function(floorplans) {
        $scope.floorplans = floorplans;
    });

});

The above doesn't work as $scope.floorplans = floorplans; is always undefined.

If I create two separate routes in my routes.php file, I can get it to work, but that seems very sloppy.

Route::get('/', function()
{

    return view('floorplans.index');

});

Route::get('test', function()
{

    return Floorplan::all();

});

What is the proper syntax to return a view with an object so that the object is picked up by my Angular controller?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire