Book Image

AngularJS Directives Cookbook

By : Monteiro
Book Image

AngularJS Directives Cookbook

By: Monteiro

Overview of this book

AngularJS directives are at the center of what makes it such an exciting – and important - web development framework. With directives, you can take greater control over HTML elements on your web pages – they ‘direct’ Angular’s HTML compiler to behave in the way you want it to. 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.
Table of Contents (11 chapters)
10
Index

The isolate $scope

The isolate scope is a key part in building custom directives; the understanding of this topic is crucial in understanding how the scope behaves between the controller and the directive.

Remember that scope inherits from the parent scope by default. This means the controller in our case, so to avoid this default behavior we need to use the isolate scope technique.

It's pretty important to avoid accidentally read or write properties in the parent scope; let's see an example.

Getting ready

We will use our latest code from the customTabs directive as a starting point to illustrate the isolate scope.

How to do it…

  1. Let's take a look at the following highlighted code from the customTabs directive; focus on the scope property:
    .directive('pane', function() {
      return {
        require: '^customTabs',
        restrict: 'E',
        transclude: true,
        scope: { title: '@' },
        link: function(scope, element, attrs, tabsCtrl) {
       ...