Tag Archives: Directive

AngularJS: Custom Directives – Using Isolated Scope

AngularJS_logo.svg

How do you pass data to your directive?

So now we have covered the most basic part of custom directives, and now you should learn how to pass data to it.

You can simple access the current scope from a controller and get the data this way.

JavaScript:

angular.module('app', [])
.controller('HelloWorldController', ['$scope',function($scope) {
    $scope.message = "This is a wonderful world we live in!"
}])
.directive('helloWorld', function () {
    return {
        restrict: 'E',
        templateUrl: 'template.html'
    };
});

Html Code:

    <body ng-controller="HelloWorldController">
        <hello-world></hello-world>
    </body>

Template:

Your message:
<b>{{message}}</b>

And then in your template you simply use the curly braces to display the message from the current scope. Continue reading

AngularJS: Custom Directives

AngularJS_logo.svgWhat is a custom directive?

In my last post I’ve described what’s a directive and the types of directives available in AngularJS. But in AngularJS you’re not restricted to what is available, you have the ability to create you own directive too.

What are the ingredients that makes up a custom directive?

A very basic custom directive will contains the module.directive API to register it. The first parameter is the name and the second parameter is the function that returns configuration object.

Below is I have created an directive that is an element with a template that display a simple Hello, World! message in html.

angular.module('app', [])
.directive('helloWorld', function () {
    return {
        restrict: 'E',
        template: '<b>Hello, World!</b>'
    };
});
<hello-world></hello-world>

Let’s break it down. Continue reading

AngularJS: Directives

AngularJS_logo.svg

What is a Directive?

AngularJS Directives is a method of manipulating the DOM and there is two types of directives, a behavior modifiers and reusable components.

Behavior Modifier Directive

This type of directive will add or modify existing behavior of the UI. Some of these types include ng-show which show or hide parts of your html code, and ng-include which allows you to include a html code from another file to be rendered in an existing UI.

Reusable Components

This type of directive can render a new html code with in your page, and it can have business logic attached to it. Your typical reusable directive would be your Tab and Accordion directives.

ng-show example

This is an attribute directive which means you can only use it as attribute in any elements. For example below I am going to you it in my DIVs.

So ng-show you have to provide a condition that will resolve to true to show part of the html:

<div ng-show="condition == true">
	Hello, World!
</div>
<div ng-show="condition == false">
	Good Bye!
</div>

In my next post I am going to talk about Custom Directive, and demonstrate how you can write you own directive for your own single page application.