简体   繁体   English

将JSON从控制器传送到Laravel中的AngularJS

[英]Convey JSON from controller to AngularJS in Laravel

In Laravel, I tried conveying data in MySQL as JSON to AngularJS. 在Laravel中,我尝试将MySQL中的数据作为JSON传输到AngularJS。 Although I was successful at that, I'm confused how to utilize the values in AngularJS. 尽管我在此方面取得了成功,但我对如何利用AngularJS中的值感到困惑。 Any help? 有什么帮助吗?

Check output data (hide each value) 检查输出数据(隐藏每个值)

在此处输入图片说明

Controller 控制者

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

AngularJS 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;
    });
});

Display 显示

<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>

Hopefully, I could fix this by myself. 希望我能自己解决这个问题。 There were 3 steps. 共有3个步骤。

  1. JSON path JSON路径
    Before $http.get('localhost:8000/json') $http.get('localhost:8000/json')
    After $http.get('/json') $http.get('/json')
  2. Set JSON to variable 将JSON设置为变量
    Before this.companies = response.data; 在此之前this.companies = response.data;
    After $scope.data = response.data; $scope.data = response.data;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM