日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

AngularJS之directive

發(fā)布時(shí)間:2023/12/29 综合教程 36 生活家
生活随笔 收集整理的這篇文章主要介紹了 AngularJS之directive 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

AngularJS之directive

  AngularJS是什么就不多舌了,這里簡單介紹下directive。內(nèi)容基本上是讀書筆記,所以如果你看過《AngularJS up and running》,那么這個(gè)可以忽略。

  1、在AngularJS中,directives有兩個(gè)主要的類型:1⃣️UI展示的修改器,如ng-show、ng-model2⃣️可復(fù)用的組件,如菜單、輪播器、taps等。
  2、directives定義:

1 angular.module('stockMarketApp', []) .directive('stockWidget', [function() {
2   return {
3     // Directive definition will go here
4 }; }]);

  需要注意的是,定義的名字采取駝峰命名,而在HTML中應(yīng)用應(yīng)該是-連接。如上面的定義應(yīng)該為:<div stock-widget></div>
  3、templateUrl,注意的是AngularJS只會(huì)在第一次碰到directive的時(shí)候去取一次,然后會(huì)緩存起來,之后都是從緩存中讀取。定義如下:

1 angular.module('stockMarketApp') .directive('stockWidget',         [function() {
2   return {
3     templateUrl: 'stock.html'
4   }; 
5 }]);

  4、如果HTML比較小的話,可以直接寫行內(nèi)HTML,放在directive定義的template屬性中。
  5、Restrict屬性:

The restrict keyword defines how someone using the directive in their code might use it. As mentioned previously, the default way of using directives is via attributes of existing elements (we used <div stock-widget> for ours).
When we create our directive, we have control in deciding how it’s used. The possible values for restrict (and thus the ways in which we can use our directive) are:
A
The letter A in the value for restrict specifies that the directive can be used as an attribute on existing HTML elements (such as <div stock-widget></div>). This is the default value.
E
The letter E in the value for restrict specifies that the directive can be used as a new HTML element (such as <stock-widget></stock-widget>).
C
The letter C in the value for restrict specifies that the directive can be used as a class name in existing HTML elements (such as <div class="stock-widget"> </div>).
M
The letter M in the value for restrict specifies that the directive can be used as HTML comments (such as <!-- directive: stock-widget -→). This was previ‐ ously necessary for directives that needed to encompass multiple elements, like multiple rows in tables, etc. The ng-repeat-start and ng-repeat-end directives were introduced for this sole purpose, so it’s preferable to use them instead of com‐ ment directives.


  其中,A是默認(rèn)的。同時(shí),既可以單獨(dú)使用,也可以多個(gè)一起用。比方說AE,既可以作為屬性,也可以作為元素單獨(dú)用。
  6、link函數(shù),對(duì)于directive來說link函數(shù)的作用跟controller對(duì)于view的作用一樣,它定義API和必要的函數(shù)。對(duì)于每一個(gè)directive的實(shí)例,AngularJS都會(huì)執(zhí)行其link函數(shù),因此包含其完整的業(yè)務(wù)邏輯,也不會(huì)影響到其它的實(shí)例。其定義會(huì)傳遞幾個(gè)固有的參數(shù),分別為directive元素的$scope、元素本身$element、元素上的屬性$attrs,定義如下:

1 link: function($scope, $element, $attrs) {}

  其中,完整定義如下:

 1 angular.module('stockMarketApp') .directive('stockWidget', [function() {
 2   return {
 3     templateUrl: 'stock.html',
 4     restrict: 'AE',
 5     link: function($scope, $element, $attrs) {
 6     $scope.getChange = function(stock) {
 7       return Math.ceil(((stock.price - stock.previous) /
 8                   stock.previous) * 100);
 9            };
10   } };
11 }]);

  7、Scope,默認(rèn)的情況下,directive都繼承其父元素的scope,并傳遞到link函數(shù)當(dāng)中。這會(huì)導(dǎo)致一些如下的問題:1⃣️新增的變量和函數(shù)會(huì)默認(rèn)修改父元素的scope,其父元素的scope莫名多了屬性和方法2⃣️可能會(huì)無意覆蓋掉父元素scope的函數(shù)或者變量3⃣️directive可以隱式的引用父元素的函數(shù)或者變量。因此,在定義directive的時(shí)候,AngularJS給了我們scope這個(gè)key,從而使我們能控制scope,其可用的值如下:

By default, each directive inherits its parent’s scope, which is passed to it in the link function. This can lead to the following problems:
? Adding variables/functions to the scope modifies the parent as well, which suddenly gets access to more variables and functions.
?? The directive might unintentionally override an existing function or variable with the same name.
? The directive can implicitly start using variables and functions from the parent. This might cause issues if we start renaming properties in the parent and forget to do it in the directive.

  注意:false是默認(rèn)的值,即使用父元素傳遞下來的scope。其中object是最強(qiáng)大的,其不繼承父元素的scope,從傳統(tǒng)scope的樹形中脫離開來,隔離開來,需要directive使用的數(shù)據(jù)需要父元素在directive引用的時(shí)候通過HTML屬性傳遞進(jìn)來,其傳遞的值可以分為暗中類別,如下:

false
This is the default value, which basically tells AngularJS that the directive scope is the same as the parent scope, whichever one it is. So the directive gets access to all the variables and functions that are defined on the parent scope, and any modifi‐ cations it makes are immediately reflected in the parent as well.
true
This tells AngularJS that the directive scope inherits the parent scope, but creates a child scope of its own. The directive thus gets access to all the variables and func‐ tions from the parent scope, but any modifications it makes are not available in the parent. This is recommended if we need access to the parent’s functions and infor‐ mation, but need to make local modifications that are specific to the directive.
object
We can also pass an object with keys and values to the scope. This tells AngularJS to create what we call an isolated scope. This scope does not inherit anything from the parent, and any data that the parent scope needs to share with this directive needs to be passed in through HTML attributes. This is the best option when cre‐ ating reusable components that should be independent of how and where they are used.

  8、Replace參數(shù),之前的所有directive在應(yīng)用到HTML中的時(shí)候都會(huì)被當(dāng)做子元素插入進(jìn)去,可是有的時(shí)候我們需要其單獨(dú)作為一個(gè)元素使用,這個(gè)時(shí)候就可以用到replace參數(shù)了。默認(rèn)設(shè)置為false,即作為子元素插入進(jìn)去。當(dāng)設(shè)置為true的時(shí)候,directive的template會(huì)替換當(dāng)前元素,同時(shí)舊元素上的屬性都會(huì)移到新元素上。

總結(jié)

以上是生活随笔為你收集整理的AngularJS之directive的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久丫精品忘忧草西安产品 | 黄色视屏在线播放 | 国产精品无码av在线播放 | 国产爽爽视频 | 91在线一区二区三区 | 国产高清精品在线 | 日韩香蕉网 | 久久你懂的 | 国产91大片 | 国产精品电影一区二区三区 | www.蜜桃av.com | 深夜福利亚洲 | 99在线精品视频免费观看软件 | 中文字幕av免费在线观看 | 日本精品一区二区在线观看 | 337p粉嫩日本欧洲亚洲大胆 | 一级黄色网 | 网站免费视频www | 久久久网| 亚洲国产成人精品无码区99 | 三级av | 国产精品综合久久久久久 | 亚洲AV无码片久久精品 | 粗了大了 整进去好爽视频 日本女优中文字幕 | 视频在线观看一区二区三区 | 国产精品综合久久 | 波多野结衣视频网站 | 松本一香在线播放 | 国产精品久久久久久亚洲毛片 | 人人草人人搞 | 亚洲综合五区 | 欧美一级视频免费 | 1769国产精品视频 | 娇妻玩4p被三个男人伺候电影 | 黄视频免费在线看 | 国产免费午夜 | 久久r| 成人在线观看免费 | 欧美另类videossexo高潮 | 好男人影视www | 九九av在线| 三级视频黄色 | 熊猫av| 天天色天天干天天 | 欧美高清在线观看 | 亚洲精品黄 | 国产精品视频网站 | 女生被男生桶 | 日韩精品无 | 91精品专区 | 992tv在线影院 | 青春草av | 中文字幕在线看高清电影 | 色综合视频在线 | 午夜精品福利一区二区 | 91黄色片| 少妇愉情理伦片bd | 超碰免费91| 亚洲乱码少妇 | 成人在线激情视频 | 色av色 | 我和我的太阳泰剧在线观看泰剧 | 四虎影院www| 麻豆国产在线播放 | sm捆绑调教视频 | 天天操操 | 国产精品视频在线免费观看 | 青青草欧美 | 婷婷视频一区二区三区 | 无码国产伦一区二区三区视频 | 白嫩日本少妇做爰 | 日韩三级麻豆 | 亚洲性视频网站 | 国产精品成av人在线视午夜片 | 欧美69囗交视频 | 黄色一级片在线 | 红桃视频成人在线 | 黄色av一区二区三区 | 青草视频网 | 精品国产乱| 女人脱下裤子让男人捅 | 国产中文字幕在线观看 | 国产精品国产三级国产播12软件 | 男生和女生靠逼视频 | 精品一卡二卡 | 91网站免费入口 | 午夜剧场免费观看 | 日韩欧美三级视频 | 香蕉视频链接 | 青草伊人久久 | 精品熟女一区二区三区 | 女人高潮娇喘声mp3 乱色视频 | 麻豆精品自拍 | 欧美xx孕妇 | 黄色com| 日批视频在线免费看 | 97在线国产 | 男女精品视频 | 国产成人不卡 |