簡體   English   中英

通過angular消費Laravel API

[英]Consuming laravel api by angular

我在laravel 5 / angular 1應用程序上工作! 我想在我的應用程序中使用數據庫信息,但在視圖中以json格式獲取它們! 這是角度控制器:

angular
.module('alerts')
.controller('alertscontroller', function($scope,$http){
$scope.alerts = [];
$http.get('{{ url('resident.alerts') }}').success(function ($data) {
              $scope.alerts = $data;
                            });
});

這是我的laravel視圖

<div class="container" ng-app="alerts" ng-controller="alertscontroller">
<div class="col-sm-3" ng-repeat="alert in alerts">
  <div class="thumbnail">
  <div class="caption">
    <h3>User X</h3>
    <p><strong>Title : </strong>@{{ alert.title }}</p>
    <p><strong>Type : </strong>@{{ alert.type }}</p>
    <p><strong>Description : </strong>@{{ alert.description }}</p>
  </div>
  </div>
</div>

我得到的結果如下

[{"id":1,"title":"Title1","description":"descriptionk,","type":"opel","status":0,"user_id":0,"created_at":"2016-08-20 21:41:00","updated_at":"2016-08-20 21:41:00"},{"id":2,"title":"Title1","description":"descriptionk,","type":"opel","status":0,"user_id":0,"created_at":"2016-08-20 21:44:05","updated_at":"2016-08-20 21:44:05"},{"id":3,"title":"Title1","description":"descriptionk,","type":"opel","status":0,"user_id":0,"created_at":"2016-08-20 21:51:54","updated_at":"2016-08-20 21:51:54"},{"id":4,"title":"Im stuck in the elevator","description":"the elevator stoped in the fourth floor when I was getting to the second floor, please help me","type":"other things","status":0,"user_id":0,"created_at":"2016-08-21 15:06:41","updated_at":"2016-08-21 15:06:41"},{"id":5,"title":"Im stuck in the elevator","description":"the elevator stoped in the fourth floor when I was getting to the second floor, please help me","type":"other things","status":0,"user_id":0,"created_at":"2016-08-21 15:08:34","updated_at":"2016-08-21 15:08:34"}]

這都是我的錯 ! 在路由部分,我有: Route::get('resident.alerts', ['uses'=>alertsController@index', 'as'=>'resident.alerts']); 而alertControllers中的index方法返回警報(return Alert :: all())! 這就是我得到結果的原因!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM