最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJs中 ng-repeat指令中實(shí)現(xiàn)含有自定義指令的動(dòng)態(tài)html的方法

 更新時(shí)間:2017年01月19日 11:25:30   作者:臉別再大了  
今天用angular寫table的時(shí)候,遇到了一個(gè)問題。在ng-repeat中,含有動(dòng)態(tài)的html,而這些html中含有自定義指令,怎么實(shí)現(xiàn)呢?下面小編給大家分享AngularJs中 ng-repeat指令中實(shí)現(xiàn)含有自定義指令的動(dòng)態(tài)html的方法,一起看看吧

今天用angular寫table的時(shí)候,遇到了一個(gè)問題。在ng-repeat中,含有動(dòng)態(tài)的html,而這些html中含有自定義指令。

因?yàn)橄M麑?shí)現(xiàn)一個(gè)能夠復(fù)用的table,所以定義了一個(gè)指令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: ["導(dǎo)入時(shí)間", "導(dǎo)入名稱", "結(jié)果", "操作"],
 theadName: ["importDate", "name", "result", "oper"]
 };
});

以上,完成了表格展示數(shù)據(jù)的功能,可是在表格列里面,經(jīng)常有對(duì)某行數(shù)據(jù)的操作,而這些操作我們同樣需要以html的形式插入到表格里面,并且這些html中,還會(huì)有ng-click等之類的指令,或者是自定義的指令。比如:"<a href='javascript:;' ng-click='show(item)'>查看信息</a>"; 這類的html,插入到td中,會(huì)以html代碼展示出來,并不會(huì)轉(zhuǎn)換成html。

在網(wǎng)上查閱了方法后,找到了一個(gè)轉(zhuǎn)html的解決辦法,增加一個(gè)過濾器,如下:

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>

通過以上方法,確實(shí)可以將html轉(zhuǎn)成正常的結(jié)果,可是a標(biāo)簽上的ng-click卻沒有效果,查看了問題的關(guān)鍵,是這段html并沒有經(jīng)過angular的編譯,所以ng-click不起作用,需要手動(dòng)編譯,修改如下:

temp文件代碼修改:

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

當(dāng)item[name] 等于 "<a href='javascript:;' ng-click='show(item)'>查看信息</a>"時(shí),我們需要通過compileBindExpn指令來手動(dòng)編譯,再放到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);
  }
 })
 }
});

經(jīng)過這種解決方法后,終于能夠正常展示html代碼,且其上的ng-click方法也能正常使用。如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能示例

    Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能示例

    這篇文章主要介紹了Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能,結(jié)合實(shí)例形式分析了AngularJS簡單控制頁面元素顯示與隱藏的相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • 基于Angularjs實(shí)現(xiàn)分頁功能

    基于Angularjs實(shí)現(xiàn)分頁功能

    這篇文章主要介紹了基于Angularjs實(shí)現(xiàn)分頁功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-05-05
  • 淺談關(guān)于angularJs中使用$.ajax的注意點(diǎn)

    淺談關(guān)于angularJs中使用$.ajax的注意點(diǎn)

    本篇文章主要介紹了關(guān)于angularJs中使用$.ajax的注意點(diǎn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Angularjs中如何使用filterFilter函數(shù)過濾

    Angularjs中如何使用filterFilter函數(shù)過濾

    這篇文章主要介紹了Angularjs中如何使用filterFilter函數(shù)過濾的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • angular-ngSanitize模塊-$sanitize服務(wù)詳解

    angular-ngSanitize模塊-$sanitize服務(wù)詳解

    本篇文章主要介紹了angular-ngSanitize模塊-$sanitize服務(wù)詳解 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Ionic + Angular.js實(shí)現(xiàn)圖片輪播的方法示例

    Ionic + Angular.js實(shí)現(xiàn)圖片輪播的方法示例

    圖片輪播在我們?nèi)粘i_發(fā)中是再熟悉不過的了,下面這篇文章主要給大家介紹了Ionic + Angular實(shí)現(xiàn)圖片輪播的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。
    2017-05-05
  • Angular8基礎(chǔ)應(yīng)用之表單及其驗(yàn)證

    Angular8基礎(chǔ)應(yīng)用之表單及其驗(yàn)證

    這篇文章主要給大家介紹了關(guān)于Angular8基礎(chǔ)應(yīng)用之表單及其驗(yàn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Angular8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Angular中樣式綁定解決方案

    Angular中樣式綁定解決方案

    這篇文章主要介紹了Angular中樣式綁定解決方案,使用ngClass和ngStyle可以進(jìn)行樣式的綁定,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 深入理解Angularjs中的$resource服務(wù)

    深入理解Angularjs中的$resource服務(wù)

    大家可以知道在Angularjs中可以用$http同服務(wù)器進(jìn)行通信,功能上比較簡單,AngularJS還提供了另外一個(gè)可選的服務(wù)$resource,使用它可以非常方便的同支持restful的服務(wù)單進(jìn)行數(shù)據(jù)交互。這篇文章主要給大家深入的介紹了Angularjs中的$resource服務(wù)。
    2016-12-12
  • AngularJs 延時(shí)器、計(jì)時(shí)器實(shí)例代碼

    AngularJs 延時(shí)器、計(jì)時(shí)器實(shí)例代碼

    這篇文章主要介紹了AngularJs 延時(shí)器、計(jì)時(shí)器實(shí)例代碼,需要的朋友可以參考下
    2017-09-09

最新評(píng)論

大厂| 佳木斯市| 嘉善县| 纳雍县| 颍上县| 邵东县| 吉安市| 和田市| 伊金霍洛旗| 修水县| 永川市| 开封市| 固始县| 韶关市| 宁化县| 西贡区| 武夷山市| 青海省| 达孜县| 白山市| 聂荣县| 平凉市| 西青区| 新兴县| 辽宁省| 房山区| 满洲里市| 江陵县| 伊川县| 蓬莱市| 甘南县| 兴海县| 泸州市| 罗源县| 延长县| 海口市| 永吉县| 遂宁市| 静乐县| 修水县| 嘉义市|