Reputation: 35
I want to get data from json
encoded variable from controller and show it to view page using angularjs
view page using angularjs
<div class="card" ng-app = "myApp">
<table class="table table-hover" ng-controller = "menuController">
<thead>
<tr>
<th>Name</th>
<th>Parent menu</th>
<th>Order</th>
<th>Status</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat = "x in values">
<td>{{x.name}}</td>
<td>{{x.parent_name}}</td>
<td>{{x.orders}}</td>
<td>{{x.status}}</td>
<td class="text-right">
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Edit row">
<i class="fa fa-pencil"></i>
</button>
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Copy row">
<i class="fa fa-copy"></i>
</button>
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Delete row">
<i class="fa fa-trash-o"></i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
var app = angular.module('myApp');
app.controller('menuController',function ($scope,$http) {
$scope.values = ["Milk", "Bread", "Cheese"];
});
PHP controller code
public function getmenu(){
$records=Menu::all();
return json_encode($records);
}
route code
Route::group(['prefix'=>'admin'],function(){
Route::get('/form','HomeController@form');
});
ErrorException (E_ERROR)
Use of undefined constant x - assumed 'x' (this will throw an Error in a future version of PHP) (View:form.blade.php)
Upvotes: 0
Views: 1368
Reputation: 3567
I think it might be because you are serving your view which contains the angular code with Laravel, and in the file form.blade.php
you use the angular syntax, which is the same as the blade one.
To solve that, you can try to remove blade
word from the view filename, so it would become form.php
or (alternative way instead of modifying the filename) each time you have to print something with the JavaScript framework instead of blade, use: @{{ variableToPrint }}
.
So for example, part of your loop would become:
<tr ng-repeat="x in values">
<td>@{{x.name}}</td>
<td>@{{x.parent_name}}</td>
<td>@{x.orders}}</td>
<td>@{{x.status}}</td>
<!-- ... -->
You get the error as blade uses the same syntax to evaluate and print values, so if you write: {{ x.name }}
blade finds a string literal x
which gets interpreted as a constant.
If you prefix the @
sign, blade will recognize that as an instruction not to be parsed, but that needs to be left as it is, it would just remove the @
, leaving you with the correct code for you JavaScript loop
Upvotes: 1