AngularJS之directive

AngularJS之directivehtml

  AngularJS是什麼就很少舌了,這裏簡單介紹下directive。內容基本上是讀書筆記,因此若是你看過《AngularJS up and running》,那麼這個能夠忽略。緩存

  一、在AngularJS中,directives有兩個主要的類型:1⃣️UI展現的修改器,如ng-show、ng-model2⃣️可複用的組件,如菜單、輪播器、taps等。
  二、directives定義:
 
1 angular.module('stockMarketApp', []) .directive('stockWidget', [function() {
2   return {
3     // Directive definition will go here
4 }; }]);
  須要注意的是,定義的名字採起駝峯命名,而在HTML中應用應該是-鏈接。如上面的定義應該爲:<div stock-widget></div>
  三、templateUrl,注意的是AngularJS只會在第一次碰到directive的時候去取一次,而後會緩存起來,以後都是從緩存中讀取。定義以下:
 
1 angular.module('stockMarketApp') .directive('stockWidget',         [function() {
2   return {
3     templateUrl: 'stock.html'
4   }; 
5 }]);
  四、若是HTML比較小的話,能夠直接寫行內HTML,放在directive定義的template屬性中。
  五、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是默認的。同時,既能夠單獨使用,也能夠多個一塊兒用。比方說AE,既能夠做爲屬性,也能夠做爲元素單獨用。
  六、link函數,對於directive來講link函數的做用跟controller對於view的做用同樣,它定義API和必要的函數。對於每個directive的實例,AngularJS都會執行其link函數,所以包含其完整的業務邏輯,也不會影響到其它的實例。其定義會傳遞幾個固有的參數,分別爲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 }]);
  七、Scope,默認的狀況下,directive都繼承其父元素的scope,並傳遞到link函數當中。這會致使一些以下的問題:1⃣️新增的變量和函數會默認修改父元素的scope,其父元素的scope莫名多了屬性和方法2⃣️可能會無心覆蓋掉父元素scope的函數或者變量3⃣️directive能夠隱式的引用父元素的函數或者變量。所以,在定義directive的時候,AngularJS給了咱們scope這個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是默認的值,即便用父元素傳遞下來的scope。其中object是最強大的,其不繼承父元素的scope,從傳統scope的樹形中脫離開來,隔離開來,須要directive使用的數據須要父元素在directive引用的時候經過HTML屬性傳遞進來,其傳遞的值能夠分爲暗中類別,以下:
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.
  八、Replace參數,以前的全部directive在應用到HTML中的時候都會被當作子元素插入進去,但是有的時候咱們須要其單獨做爲一個元素使用,這個時候就能夠用到replace參數了。默認設置爲false,即做爲子元素插入進去。當設置爲true的時候,directive的template會替換當前元素,同時舊元素上的屬性都會移到新元素上。 
相關文章
相關標籤/搜索