AngularJS directives are at the center of what makes it such an exciting – and important - web development framework. With directives, you can take greater. AngularJS directives are at the center of what makes it such an exciting – and important - web development framework. With directives, you can. This book is for sale at medical-site.info . demo: http:// medical-site.info
|Language:||English, Spanish, French|
|Genre:||Science & Research|
|Distribution:||Free* [*Register to download]|
medical-site.info AngularJS medical-site.info http:// medical-site.info Directives medical-site.info medical-site.info - Ebook download as PDF File .pdf), Text File .txt) or read You created an html string with some AngularJS directives inside your link . medical-site.info - Download as PDF File .pdf), Text File Angular interprets those attributes as directives to bind input or output parts of.
These directives can expose methods as follows: 25 www.
Using , , and , you are instructing the directive to ignore the scopes it would normally inherit, and only utilize data, variables, and methods that you have provided interfaces for instead. There's more… If the directive is designed as a speci c modi er for an aspect of your application, you might nd that using isolate scope isn't necessary.
On the other hand, if you're building a reusable, monolithic component that can be reused across multiple applications, it is unlikely that the directive will be using the parent scope in which it is used. Hence, isolate scope will be signi cantly more useful. See also f The Recursive directives recipe utilizes the isolate scope to maintain inheritance and separation in a recursive DOM tree Interaction between nested directives AngularJS provides a useful structure that allows you to build channels of communication between directive siblings within the same HTML element or parents in the same DOM ancestry without having to rely on AngularJS events.
Getting ready For this recipe, suppose that your application template includes the following: 24 www. In the same way that a model variable can be bound to the child scope, you can alias methods that are de ned in the parent scope to be invoked from the child scope but are still in the parent scope context.
This is accomplished with the de nition, as follows: JSFiddle: Here, you are instructing the child directive to evaluate the expression passed to the attribute within the context of the parent controller. In this case, the expression will invoke the method, but any valid AngularJS expression will also work. You can invoke it as you would invoke any other scope method, including parameters as required. What is far more likely to be useful to you is a true whitelist of the data binding from the parent scope.
This can be accomplished with the de nition, as follows: JSFiddle: Here, you are instructing the child directive scope to examine the parent controller scope, and bind the parent attribute inside the child scope, aliased as the attribute. Full data binding between scopes is supported, and all unnamed attributes and methods in the parent scope are ignored.
If you want to pass a read-only value to the directive, you will use inside the isolate scope declaration to indicate that a named attribute of the relevant HTML element contains a value that should be incorporated into the directive's isolate scope.
This can be done as follows: With this, the scope inside the directive now contains an attribute with the value of in the parent scope. AngularJS evaluates the expression string, and the result is provided to the directive's scope.
Setting the value of the variable does nothing to the parent scope or the attribute in the HTML; it is merely copied into the scope of the directive. This is especially true in the context of directives, as they are subject to the scopes they are inserted into and, therefore, require careful management in order to prevent unexpected functionalities.
Fortunately, AngularJS directives afford several robust tools that help manage visibility of and interaction with the surrounding scopes. If a directive is not instructed to provide a new scope for itself, it will inherit the parent scope.
In the case that this is not desirable behavior, you will need to create an isolate scope for that directive, and inside that isolate scope, you can de ne a whitelist of parent scope elements that the directive will need.
Getting ready For this recipe, assume your directive exists inside the following setup: 20 www.
You can see that it has injected into it, as you need to de ne event listeners relevant to this directive all across. Here, a very simple template is de ned, which would preferably be in its own le, but for the sake of simplicity, it is merely incorporated as a string. This directive rst initializes the element with some basic CSS in order to have the relevant anchor point somewhere you can move the cursor around fully.
This value is taken from an element attribute in the same fashion it was used in the previous recipe. Here, our directive is listening to a event, with a handler inside wrapped in the wrapper. If you remove this wrapper and test the directive, you will notice that while the handler code does execute, the DOM does not get updated.
This is because the event that the application is listening for does not occur in the AngularJS context—it is merely a browser DOM event, which AngularJS does not listen for. In order to inform AngularJS that models might have been altered, you must utilize the wrapper to trigger the update of the DOM.
With all of this, your cursor movement should constantly be invoking the event handler, and you should see a real-time description of your cursor's relative cardinal locality. There's more… In this directive, we have used the parameter for the rst time. You might be wondering, "Which scope am I using? I haven't declared any speci c scope anywhere else in the application. If you were to inject to the directive and log to the console inside the event handler, you would see that this directive is writing to the attribute of the of the entire application!
See also f The Isolate scope recipe goes into further details on directive scope management 19 www. The element parameter provided to you is already packaged as a jqLite object, so you are free to inspect and modify it at your will. In this example, you are manually increasing the integer value of a counter, the result of which is inserted as text inside the button. There's more… Here, it's important to note that you will never need to modify the DOM in your controller, whether it is a directive controller or a general application controller.
However, managing the DOM transformation out of place causes an undesirable dependency between the controller and the DOM they should be totally decoupled as well as makes testing more dif cult. Directives are tailor-made to layer and group DOM modi cation tasks, and you should have no trouble using them as such. Additionally, it's worth mentioning that the object is read-only, and you cannot set attributes through this channel. It's still possible to modify attributes using the element attribute, but state variables for elements can be much more elegantly implemented, which will be discussed in a later recipe.
See also f In this recipe, you saw the function used for the rst time in a fairly rudimentary fashion. The next recipe, Linking directives, goes into further detail.
Linking directives For a large subset of the directives you will eventually build, the bulk of the heavy lifting will be done inside the directive's function.
This function is returned from the preceding compile function, and as seen in the previous recipe, it has the ability to manipulate the DOM in and around it. Here, your directive is attached to a singular element that the directive sees in its linking function as the element parameter. You are able to de ne your DOM modi cation logic here, which includes initial element modi cation and the setup of events.
Since the release of AngularJS 1. Nevertheless, they can still be employed effectively. How it works… AngularJS actively compiles the template, searching for matches to de ned directives. It's possible to chain directive forms together within the same de nition.
The directive with can appear as follows: There's more… The statements in this recipe should have given you some insight into the extraordinary use that directives can have in your application. See also f The Interaction between nested directives recipe demonstrates how to allow directives attached to the same element to communicate with each other Manipulating the DOM In the previous recipe, you built a directive that didn't care what it was attached to, what it was in, or what was around it.
Directives exist for you to program the DOM, and the equivalent of the last recipe is to instantiate a variable. In this recipe, you will actually implement some logic. Additionally, a value can be directly applied to the directive class name attribute by passing it in the CSS string. The comment directive Comment directives are the runt of the group.
You will very infrequently nd their use necessary, but it's useful to know that they are available in your application. This directive can be used in a template in the following fashion: The comment directive can be de ned as follows: JSFiddle: 14 www. It assumes its attribute values from the container element's attributes, including the attribute directive and other directives whether or not they are assigned a value.
The class directive Class directives are not altogether that different from attribute directives. They provide the ability to have multiple directive assignments, unrestricted local attribute value access, and local directive communication. This directive can be used in a template in the following fashion: This attribute directive can be de ned as follows: JSFiddle: 13 www.
That being said, pretty much every modern browser will have no problem if you leave it out. The attribute directive can be de ned as follows: JSFiddle: 12 www. If you want to replace the tag entirely with the content instead, the directive will be de ned as follows: JSFiddle: This approach will operate in an identical fashion, but the directive's inner HTML will not be wrapped with tags in the compiled HTML. Also, note that the logged template is missing its tags that have become the root directive element as they are the top-level tags inside the template.
The attribute directive Attribute directives are the most commonly used form of directives, and for good reason. They have the following advantages: f They can be added to existing HTML as standalone attributes, which is especially convenient if the directive's purpose doesn't require you to break up an existing template into fragments 11 www. The functions have three parameters by default: the directive scope which you will learn more about later , the relevant DOM element, and the element's attributes as key-value pairs.
The directive can be used in a template in the following fashion: This will result in the directive template replacing the wrapped contents of the tag with the template. This element directive can be de ned as follows: 10 www.
AngularJS traverses the DOM tree of the page to look for directives among many other things that it needs to perform an action for. Here, AngularJS looks at the element, locates the relevant template in , and inserts it into the page for the browser to handle. The provided template will be compiled in the same way, so the use of and other AngularJS directives is fair game, as demonstrated here. There's more… A directive in this fashion, though useful, isn't really what directives are for.
It provides a nice jumping-off point and gives you a feel of how it can be used. However, the purpose that your custom directive is serving can be better implemented with the built-in directive, which inserts a template into the designated part of HTML. This is not to say that directives shouldn't ever be used this way, but it's always good practice to not reinvent the wheel. It makes building modern web applications a much more expressive experience, and allows you to focus more closely on improving the way that user interaction impacts the DOM and the way your app manages data.
If you're already using Angular, you probably recognize the power of directives to transform the way you understand and build your projects - but customizing and creating your own directives to harness AngularJS to its full potential can be more challenging. This cookbook shows you how to do just that - it's a valuable resource that demonstrates how to use directives at every stage in the workflow.
Packed with an extensive range of solutions and tips that AngularJS developers shouldn't do without, you'll find out how to make the most of directives. You'll find recipes demonstrating how to build a number of different user interface components with directives, so you can take complete control over how users interact with your application.
You'll also learn how directives can simplify the way you work by creating reusable directives - by customizing them with Yeoman you can be confident that you're application has the robust architecture that forms the bedrock of the best user experiences.
You'll also find recipes that will help you learn how to unit test directives, so you can be confident in the reliability and performance of your application. Whether you're looking for guidance to dive deeper into AngularJS directives, or you want a reliable resource, relevant to today's web development challenges, AngularJS Directives Cookbook delivers everything you need in an easily accessible way.
Style and approach This book easy-to-follow guide is packed with hands-on recipes to help you build modular AngularJS applications with custom directives. It presents tips on using the best tools and various ways to use these tools for front-end development.