AngularJS之directive
AngularJS之directive
AngularJS是什么就不多舌了,這里簡單介紹下directive。內容基本上是讀書筆記,所以如果你看過《AngularJS up and running》,那么這個可以忽略。
1、在AngularJS中,directives有兩個主要的類型:1⃣️UI展示的修改器,如ng-show、ng-model2⃣️可復用的組件,如菜單、輪播器、taps等。
2、directives定義:
1 angular.module('stockMarketApp', []) .directive('stockWidget', [function() {
2 return {
3 // Directive definition will go here
4 }; }]);
需要注意的是,定義的名字采取駝峰命名,而在HTML中應用應該是-連接。如上面的定義應該為:<div stock-widget></div>
3、templateUrl,注意的是AngularJS只會在第一次碰到directive的時候去取一次,然后會緩存起來,之后都是從緩存中讀取。定義如下:
1 angular.module('stockMarketApp') .directive('stockWidget', [function() {
2 return {
3 templateUrl: 'stock.html'
4 };
5 }]);
4、如果HTML比較小的話,可以直接寫行內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是默認的。同時,既可以單獨使用,也可以多個一起用。比方說AE,既可以作為屬性,也可以作為元素單獨用。
6、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 }]);
7、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.
8、Replace參數,之前的所有directive在應用到HTML中的時候都會被當做子元素插入進去,可是有的時候我們需要其單獨作為一個元素使用,這個時候就可以用到replace參數了。默認設置為false,即作為子元素插入進去。當設置為true的時候,directive的template會替換當前元素,同時舊元素上的屬性都會移到新元素上。
總結
以上是生活随笔為你收集整理的AngularJS之directive的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OTU(operational taxo
- 下一篇: JAXB 实现java对象与xml之间互