簡體   English   中英

將JSON從控制器傳送到Laravel中的AngularJS

[英]Convey JSON from controller to AngularJS in Laravel

在Laravel中,我嘗試將MySQL中的數據作為JSON傳輸到AngularJS。 盡管我在此方面取得了成功,但我對如何利用AngularJS中的值感到困惑。 有什么幫助嗎?

檢查輸出數據(隱藏每個值)

在此處輸入圖片說明

控制者

public function index(){
  return Response::json(Company::all());
}

AngularJS

var app = angular.module('companySearchApp', [], function($interpolateProvider) {
    $interpolateProvider.startSymbol('<%');
    $interpolateProvider.endSymbol('%>');
});

app.controller('CompanyCtrl', function($scope, $http){
  $http.get('http://localhost:8000/json')
    .then(function(response){
      this.companies = response.data;
    });
});

顯示

<div id="resultTable" align="center">
  <h3>Search Result</h3>

  <div ng-repeat="company in CompanyCtrl.companies">
    <table border="1" style="margin-bottom:10px">
      <tr>
        <td colspan="3" class="col-xs-12"><% company.CompanyName %></td>
      </tr>
      <tr>
        <td class="col-xs-4"><% company.City %></td>
        <td class="col-xs-4"><% company.Province %></td>
        <td class="col-xs-4"><% company.KeyWord %></td>
      </tr>
      <tr>
        <td colspan="3"><% company.JobTitle %></td>
      </tr>
      <tr>
        <td colspan="3"><% company.Link %></td>
      </tr>
    </table>
  </div>
</div>

希望我能自己解決這個問題。 共有3個步驟。

  1. JSON路徑
    $http.get('localhost:8000/json')
    $http.get('/json')
  2. 將JSON設置為變量
    在此之前this.companies = response.data;
    $scope.data = response.data;

  3. ng-repeat
    <div ng-repeat="company in CompanyCtrl.companies">之前<div ng-repeat="company in CompanyCtrl.companies">
    <div ng-repeat="company in data">

暫無
暫無

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

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