Angular和Laravel - 从mysql获取数据

时间:2015-09-11 06:14:34

标签: javascript php angularjs laravel laravel-5

我需要从我的数据库中获取数据,更新div并onclick更新另一个div 我设法获取数据,但我无法通过onCLick加载额外的数据。

我的代码:

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

app.controller('mainController', function($scope, $http) {

$scope.fetcheddata = [];
$scope.loading = false;

$scope.init = function() {
    $scope.loading = true;
    $http.get('/api/content').
    success(function(data, status, headers, config) {
        $scope.fetcheddata = data;
            $scope.loading = false;

    });
}
$scope.getDesc = function() {
    $scope.loading = true;
    $http.get('/api/content/get/' + item.id).
    success(function(data, status, headers, config) {
        $scope.fetcheddata = data;
            $scope.loading = false;

    });
}

$scope.init();

});

查看代码:

 <div class="col-xs-2 type-image" tr ng-repeat='item in fetcheddata'>
     <a href="#" class="thumbnail click-productgroup" data-group="show-<% item.group %>">
       <img src="<% item.image %>" class="img-responsive">
     </a>
  </div>  

-tag应该获取item.description并更新另一个div。我怎么能这样做?
我尝试使用getDesc范围。这是正确的吗?但是我如何更新div?

编辑1:

                <div class="row">
                <div class="col-md-7" id="productGroups">
                        <div class="row type-row">

                            <div class="col-xs-2 type-image" ng-repeat='item in fetchedData'>
                                <a href="#" class="thumbnail" ng-click='getDesc(<% item.id %>)'>
                                    <img src="<% item.image %>" class="img-responsive">
                                </a>
                            </div>

                        </div>
                </div>
                <div class="col-md-5 type-description">
                    <% fetchedDesc | json %>
                </div>
            </div>  

并在我的控制器中:

$scope.getDesc = function() {
    $scope.loading = true;
    $http.get('/api/content/get/' + item.id).
    success(function(data, status, headers, config) {
        $scope.fetchedDesc = data.description;
            $scope.loading = false;

    });
}  

仍然页面跳转点击到顶部并且div中没有​​内容(尽管ng-binding出现在html中)。

1 个答案:

答案 0 :(得分:1)

我注意到你的getDesc没有通过item.id

$scope.getDesc = function(item) {
 $http.get('/api/content/get/' + item.id)
   .then(function(data) {
      console.log(data)
   })
}

这应该来自ng-click代码的div

 <div class="col-xs-2 type-image" tr ng-repeat='item in fetcheddata'>
     <a href="#" class="thumbnail click-productgroup" data-group="show-<% item.group %>" ng-click="getDesc(item)">
       <img src="<% item.image %>" class="img-responsive">
     </a>
  </div>