繁体   English   中英

来自PHP的Angular JS动态HTML ng-单击不起作用

[英]Angular JS dynamic HTML from PHP ng-click not working

我是Angular JS的新手,

我想用PHP动态制作HTML中的ng-click事件,这是代码。

var app = angular.module('pageapp',[]);

angular.module('pageapp')
.filter('to_trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
}]);

app.controller('productList',function($scope,$http){
  $http.get("../webservice/api/get-products")
  .success(function(response){ 
    $scope.products = response.data; 
    $scope.paginationLinks = response.links;        
 });

 $scope.getPageData = function () {
    alert("Hello");
 }
});

HTML ...

<section ng-controller="productList">
<table class="table table-responsive table-hover">
    <thead>
        <tr>
            <th>S.No</th>
            <th>Name</th>
            <th>Color</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="product in products">
            <td>{{$index + 1}}</td>
            <td>{{product.name}}</td>
            <td>{{product.color}}</td>
            <td>{{product.price}}</td>
        </tr>
    </tbody>
</table>
<ul class="pagination pull-right" ng-bind-html="paginationLinks|to_trusted""></ul>
</section>

PHP ..

public function Products(){
    $start = 0;
    $perPage = 10;
    $count = DB::table('products')->count();
    $totalPages = ceil($count/$perPage);
    $data = DB::table('products')
            ->select('name','price','color')
            ->take($perPage)
            ->skip($start)
            ->get();

    for ($i=1; $i <= $totalPages; $i++) { 
        if($i == 1)
            $links ="<li><a href=''>".$i."</a></li>";
        else
            $links .="<li><a href='' ng-click='getPageData(".$i.")'>".$i."</a></li>";
    }

    return [
        'totalpages'    =>$totalPages,
        'data'          =>$data,
        'links'         =>$links
    ];
}

无需在PHP中添加ng-click='getPageData(".$i.")ng-click="getPageData($event)"ng-click="getPageData($event)"到HTML中的ul元素中。 因此,您可以在JS中获得被点击的li元素(通过事件冒泡 ),您可以做任何您想做的事情。

希望能帮助到你 :)

暂无
暂无

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

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