AngularJS之directivehtml
AngularJS是什麼就很少舌了,這裏簡單介紹下directive。內容基本上是讀書筆記,因此若是你看過《AngularJS up and running》,那麼這個能夠忽略。緩存
1 angular.module('stockMarketApp', []) .directive('stockWidget', [function() { 2 return { 3 // Directive definition will go here 4 }; }]);
1 angular.module('stockMarketApp') .directive('stockWidget', [function() { 2 return { 3 templateUrl: 'stock.html' 4 }; 5 }]);
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.
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 }]);
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 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.