I'm trying to figure out something. Most of the time, the way I create a RESTful API is by doing something like this:
Route:
Route::get('/news', 'NewsController@show');
Controller:
class NewsController extends Controller
{
public function show()
{
$news= News::all();
return view('newsview', compact('news'));
}
}
Blade:
@foreach ($news as $n)
<li> {{$n->title}} {{$n->author}}</li>
@endforeach
And now, I'm trying to learn AngularJS
. The way I see some tutorials on the web is they are using AngularJS
as front-end
and Laravel
as the back-end
. And now I see something like this:
Route:
Route::get('/news', 'NewsController@show');
Controller:
class NewsController extends Controller
{
public function show()
{
return News::all();
}
}
HTML/JS:
<div ng-app="myApp" ng-controller="newsCtrl">
<ul>
<li ng-repeat="x in news">
{{ x.title + ', ' + x.author }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('newsCtrl', function($scope, $http) {
$http.get("/news")
.success(function(response) {$scope.news = response.records;});
});
</script>
What's the advantage of using one to another? I'm really interested in using AngularJS
.