AngularJs中 ng-repeat指令中實現含有自訂指令的動態html的方法,angularjsng-repeat

來源:互聯網
上載者:User

AngularJs中 ng-repeat指令中實現含有自訂指令的動態html的方法,angularjsng-repeat

今天用angular寫table的時候,遇到了一個問題。在ng-repeat中,含有動態html,而這些html中含有自訂指令。

因為希望實現一個能夠複用的table,所以定義了一個指令myStandTable,指令代碼大概如下:

var myCommon = angular.module("myCommon",[]);myCommon.directive("myStandTable", function () { return { restrict: "A", templateUrl: "app/template/tableTem.html", transclude: false, replace: true, controller: function ($scope,$compile, commonService) {  // do something...  }, link: function (scope, element, attris) { } }});

tableTem.html檔案代碼如下:

<div> <table class="table table-hover table-bordered"> <thead>  <tr>  <th ng-if="tableData.multiSelect">   <input type="checkbox" id="check-all" ng-model="itemsChecked">   <label for="check-all" class="fa" ng-class="{'fa-square-o': !itemsChecked, 'fa-check-square-o': itemsChecked }" aria-hidden="true">   </label>  </th>  <th ng-repeat="item in tableData.thead">{{item}}</th>  </tr> </thead> <tbody>  <tr ng-repeat="item in tableData.items" ng-click="selectItem(item)" ng-init="item.selected = false" ng-class="{'selected': item.selected}">  <td ng-if="tableData.multiSelect">   <input type="checkbox" id="check_{{$index}}" ng-model="item.selected">   <label for="check_{{$index}}" class="fa" ng-class="{'fa-square-o': !item.selected, 'fa-check-square-o': item.selected }" aria-hidden="true">   </label>  </td>  <td ng-repeat="name in tableData.theadName">   {{item[name]}}     </td>  </tr> </tbody> </table></div>

控制器檔案代碼如下:

var myBasis = angular.module("myBasis",["myCommon"]);myBasis.controller("myCtrl", function ($scope) { $scope.tableData = { multiSelect: false, pageSize: [10, 20, 50], thead: ["匯入時間", "匯入名稱", "結果", "操作"], theadName: ["importDate", "name", "result", "oper"] };});

以上,完成了表格展示資料的功能,可是在表格列裡面,經常有對某行資料的操作,而這些操作我們同樣需要以html的形式插入到表格裡面,並且這些html中,還會有ng-click等之類的指令,或者是自訂的指令。比如:"<a href='javascript:;' ng-click='show(item)'>查看資訊</a>"; 這類的html,插入到td中,會以html代碼展示出來,並不會轉換成html。

在網上查閱了方法後,找到了一個轉html的解決辦法,增加一個過濾器,如下:

myCommon.filter("trusted", function ($sce) { return function (html) { if (typeof html == "string") {  return $sce.trustAsHtml(html); } return html; }});

然後修改temp檔案中的代碼:

 <td ng-repeat="name in tableData.theadName"> <span ng-bind-html="item[name] | trusted"></span></td>

通過以上方法,確實可以將html轉成正常的結果,可是a標籤上的ng-click卻沒有效果,查看了問題的關鍵,是這段html並沒有經過angular的編譯,所以ng-click不起作用,需要手動編譯,修改如下:

temp檔案代碼修改:

 <td ng-repeat="name in tableData.theadName"> <div compile-bind-expn = "item[name]"> </div> </td>

當item[name] 等於 "<a href='javascript:;' ng-click='show(item)'>查看資訊</a>"時,我們需要通過compileBindExpn指令來手動編譯,再放到div裡面去。指令代碼如下:

myCommon.directive("compileBindExpn", function ($compile) { return function linkFn(scope, elem, attrs) { scope.$watch("::"+attrs.compileBindExpn, function (html) {  if (html && html.indexOf("<") != -1 && html.indexOf(">") != -1) {  console.log(1);  var expnLinker = $compile(html);  expnLinker(scope, function transclude (clone) {   elem.empty();   elem.append(clone);  });  } else {  elem.empty();  elem.append(html);  } }) }});

經過這種解決方案後,終於能夠正常展示html代碼,且其上的ng-click方法也能正常使用。如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.