Angular使用$compile為從Ajax加載的HTML綁定ng-click事件


這是一個Angular使用$compile為從Ajax加載的HTML綁定ng-click事件的實現方式,由於近期忙碌,就先放代碼。代碼如下:

<table data-ng-table="tableParams" class="table table-bordered table-hover " style="border-collapse:collapse" data-ng-init="host.editSave = false">
    <tr id="newTransaction"></tr>
    <tr data-ng-repeat="host in hosts|filter:search:strict">
        <td class="hostTableCols" data-ng-hide="host.editSave">{{host.hostCd}}</td>
        <td class="hostTableCols" data-ng-hide="host.editSave">{{host.hostName}}</td>
    </tr>
</table>

js:

var app = angular.module('my-app', [], function () {

})

app.controller('AppController', function ($scope, $compile) {

    var $el = $('<td contenteditable><input type="text" class="editBox" value=""/></td>' +
        '<td contenteditable><input type="text" class="editBox" value=""/></td>' +
        '<td>' +
        '<span>' +
        '<button id="createHost" class="btn btn-mini btn-success" data-ng-click="create()"><b>Create</b></button>' +
        '</span>' +
        '</td>').appendTo('#newTransaction');
    $compile($el)($scope);
    
    $scope.create = function(){
        console.log('clicked')
    }
})

  PS:如果有寫錯的地方,歡迎指出,謝謝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM