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

angularJs關(guān)于指令的一些冷門屬性詳解

 更新時(shí)間:2016年10月24日 10:04:22   投稿:jingxian  
下面小編就為大家?guī)硪黄猘ngularJs關(guān)于指令的一些冷門屬性詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

我們使用ng的時(shí)候,經(jīng)常會使用到指令,大家所熟知的屬性我在這里就不介紹了,講講大家沒怎么留意的屬性

1.multiElement

這是指定指令作用區(qū)間的功能,最常用的就是ng-repeat-start和ng-repeat-end了。

2.priority

指令優(yōu)先級,優(yōu)先級越高,指令越早執(zhí)行。

3.terminal

是否允許優(yōu)先級低的指令起作用,如果是true,那么只有比當(dāng)前指令或跟當(dāng)前指令等級相同的指令才可以執(zhí)行。最典型的就是ngIf

4.templateNamespace

聲明模板的格式有三種選擇 svg、html、math

5.transclude

或許有人疑問了,transclude也算是冷門屬性嗎?其實(shí)大家對transclude了解并沒有想象的那么深,transclude是一個(gè)挺復(fù)雜的屬性,一般大家會用到的也僅僅是true,false。這兩個(gè)屬性我在這里就不講了,在這里我主要講的是transclude:element,我google了一整天都沒找到正確描述這個(gè)屬性的方法。我覺得google出來的答案太文檔化了。最后在研究$transclude才看出來這個(gè)屬性的功能究竟在哪里。再講功能前我們先了解下$transclude

無論在指令的compile還是link時(shí)期我們的最后一個(gè)參數(shù)就是$transclude了,這里其實(shí)我們看看源碼是如何定義的,我看的源碼是ng1.5.3的

function controllersBoundTransclude(scope, cloneAttachFn, futureParentElement, slotName) {
     var transcludeControllers;
     // No scope passed in:
     if (!isScope(scope)) {
      slotName = futureParentElement;
      futureParentElement = cloneAttachFn;
      cloneAttachFn = scope;
      scope = undefined;
     }

     if (hasElementTranscludeDirective) {
      transcludeControllers = elementControllers;
     }
     if (!futureParentElement) {
      futureParentElement = hasElementTranscludeDirective ? $element.parent() : $element;
     }
     if (slotName) {
      // slotTranscludeFn can be one of three things:
      // * a transclude function - a filled slot
      // * `null` - an optional slot that was not filled
      // * `undefined` - a slot that was not declared (i.e. invalid)
      var slotTranscludeFn = boundTranscludeFn.$$slots[slotName];
      if (slotTranscludeFn) {
       return slotTranscludeFn(scope, cloneAttachFn, transcludeControllers, futureParentElement, scopeToChild);
      } else if (isUndefined(slotTranscludeFn)) {
       throw $compileMinErr('noslot',
        'No parent directive that requires a transclusion with slot name "{0}". ' +
        'Element: {1}',
        slotName, startingTag($element));
      }
     } else {
      return boundTranscludeFn(scope, cloneAttachFn, transcludeControllers, futureParentElement, scopeToChild);
     }
    }

還有一個(gè)另一個(gè)函數(shù)要特別指出來,就是最后返回的 boundTranscludeFn 這個(gè)方法,下面是他的源碼

function createBoundTranscludeFn(scope, transcludeFn, previousBoundTranscludeFn) {
   function boundTranscludeFn(transcludedScope, cloneFn, controllers, futureParentElement, containingScope) {

    if (!transcludedScope) {
     transcludedScope = scope.$new(false, containingScope);
     transcludedScope.$$transcluded = true;
    }

    return transcludeFn(transcludedScope, cloneFn, {
     parentBoundTranscludeFn: previousBoundTranscludeFn,
     transcludeControllers: controllers,
     futureParentElement: futureParentElement
    });
   }

這兩個(gè)方法到底是在做什么呢?其實(shí)就是克隆了當(dāng)前指令的節(jié)點(diǎn),并生成子作用域??寺〉墓?jié)點(diǎn)由transclude定義,如果你的屬性是true,則克隆的是指令模板中的ng-transclude所在的DOM節(jié)點(diǎn),及其子節(jié)點(diǎn)。如果屬性是element則克隆整個(gè)模板的節(jié)點(diǎn)。

這是兩個(gè)指令的代碼

angular.module('MyApp', [])
      .directive('dropPanel', function() {
        return {
          transclude: 'element',
          replace: true,
          template: "<div class='drop-panel'>" +
            "<span ng-transclude class='111'></span>" +
            "</div>",
          link: function(scope, el, c, d, $transclude) {
            $transclude(function ngRepeatTransclude(clone, scope) {
              console.log(clone);
            })

          }
        }
      })
      .directive('dropPanel2', function() {
        return {
          transclude: true,
          replace: true,
          template: "<div class='drop-panel'>" +
            "<span ng-transclude class='111'></span>" +
            "</div>",
          link: function(scope, el, c, d, $transclude) {
            $transclude(function ngRepeatTransclude(clone, scope) {
              console.log(clone);
            })
          }
        }
      })

如果你覺得replace干擾了對結(jié)果的理解,你可以注釋掉,然后查看控制臺中打印出來的clone,你就能知道所謂transclude的屬性聲明為element的作用了,我們打開replace目的在于能較清楚的查看DOM節(jié)點(diǎn),來獲得結(jié)論,下面就是兩者編譯后DOM節(jié)點(diǎn)的區(qū)別了

看完上面的圖,你可以明顯的區(qū)別到兩者對DOM的克隆不一樣的,另外如果在聲明屬性為‘element'時(shí),需要聲明replace為true,才能渲染出來。我查了很多資料,最終用斷點(diǎn)得出了我認(rèn)為對的結(jié)論,斷點(diǎn)追蹤的結(jié)果是發(fā)現(xiàn)如果不聲明replace,好像就不會執(zhí)行ngTransclude指令,這點(diǎn)我很奇怪,正因?yàn)檫@樣子所以導(dǎo)致沒有成功渲染。二歸根結(jié)底其實(shí)是兩者的操作的DOM元素不同,在聲明transclude為element時(shí),replace為true,你取到的DOM節(jié)點(diǎn)是含有transclude屬性的節(jié)點(diǎn)(子節(jié)點(diǎn)),而為false你拿到的并不是含有transclude屬性的節(jié)點(diǎn)(父節(jié)點(diǎn)),而ng本身不對其節(jié)點(diǎn)進(jìn)行遍歷,導(dǎo)致沒能執(zhí)行ngTransclude指令

我看到一個(gè)觀點(diǎn)覺得不錯,大概意思就是:源于功能的考慮,在使用element屬性的時(shí)候,一般都是起占位符的作用,你需要做的操作是對DOM的添加時(shí)候,才會用到這個(gè)克隆功能。

我覺得這個(gè)觀點(diǎn)不錯,看過很多關(guān)于ngrepeat的介紹,很多文章都說ngrepeat源碼是通過$scope.$new()來生成子作用域的,實(shí)際上并不完全正確,他的確是通過$scope.$new產(chǎn)生子作用域的,但是這個(gè)產(chǎn)生功能是交給$transclude函數(shù)去做得,實(shí)際上ngrepeat的源碼上是通過$transclude來生成子作用域和添加DOM節(jié)點(diǎn)的。與上面的觀點(diǎn)有相似之處。

以上就是小編為大家?guī)淼腶ngularJs關(guān)于指令的一些冷門屬性詳解全部內(nèi)容了,希望大家多多支持腳本之家~

  • Angular應(yīng)用里異步打開對話框技術(shù)詳解

    Angular應(yīng)用里異步打開對話框技術(shù)詳解

    這篇文章主要為大家介紹了Angular應(yīng)用里異步打開對話框技術(shù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • AngularJs Understanding the Controller Component

    AngularJs Understanding the Controller Component

    本文主要介紹AngularJs Understanding the Controller Component的內(nèi)容,這里整理了相關(guān)資料,及簡單示例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • Angular 獨(dú)立組件入門指南

    Angular 獨(dú)立組件入門指南

    這篇文章主要為大家介紹了Angular 獨(dú)立組件入門教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • AngularJS表單基本操作

    AngularJS表單基本操作

    這篇文章主要為大家詳細(xì)介紹了AngularJS表單基本操作的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • AngularJS單選框及多選框?qū)崿F(xiàn)雙向動態(tài)綁定

    AngularJS單選框及多選框?qū)崿F(xiàn)雙向動態(tài)綁定

    這篇文章主要為大家詳細(xì)介紹了AngularJS單選框及多選框?qū)崿F(xiàn)雙向動態(tài)綁定的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能

    Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能

    本文通過實(shí)例代碼給大家介紹了Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能,非常不錯,具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-09-09
  • AngularJS中實(shí)現(xiàn)顯示或隱藏動畫效果的方式總結(jié)

    AngularJS中實(shí)現(xiàn)顯示或隱藏動畫效果的方式總結(jié)

    AngularJS 是一組用于創(chuàng)建單頁Web應(yīng)用的豐富框架,給構(gòu)建豐富交互地應(yīng)用帶來了所有功能,其中一項(xiàng)主要的特性是Angular對動畫的支持。下面通過本文給大家介紹AngularJS中實(shí)現(xiàn)顯示或隱藏動畫效果的方式總結(jié),對angularjs動畫效果相關(guān)知識感興趣的朋友一起學(xué)習(xí)
    2015-12-12
  • Angluar+zorro實(shí)現(xiàn)無限級菜單

    Angluar+zorro實(shí)現(xiàn)無限級菜單

    這篇文章主要為大家詳細(xì)介紹了Angluar+zorro實(shí)現(xiàn)無限級菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Angular?ViewChild組件間通信demo

    Angular?ViewChild組件間通信demo

    這篇文章主要為大家介紹了Angular?ViewChild組件間通信demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 最新評論

    台南县| 丰原市| 昌黎县| 三江| 高要市| 察雅县| 呼图壁县| 冀州市| 昌平区| 名山县| 天峨县| 嵊州市| 舞钢市| 屏东市| 新巴尔虎左旗| 新兴县| 望城县| 天等县| 海城市| 勐海县| 万荣县| 光山县| 迁西县| 历史| 丹江口市| 沛县| 新乡市| 长武县| 山西省| 玛曲县| 伊春市| 天门市| 苍梧县| 大埔县| 都昌县| 安福县| 宜阳县| 南昌市| 麻城市| 磐安县| 霸州市|