Angularjs-directive

1y ago
10 Views
2 Downloads
847.09 KB
9 Pages
Last View : 5d ago
Last Download : 3m ago
Upload by : Camille Dion
Transcription

angularjs-directive #angularjsdirective

Table of Contents About 1 Chapter 1: Getting started with angularjs-directive 2 Remarks 2 Examples 2 Installation or Setup 2 Building a reusable component 2 Your first directive 3 Success/Error pop-up message using simple link function 4 Chapter 2: Commonly Used Directives Examples ngConfirmClick: Confirm before evaluating expression. Credits 6 6 6 7

About You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: angularjs-directive It is an unofficial and free angularjs-directive ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official angularjsdirective. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to info@zzzprojects.com https://riptutorial.com/ 1

Chapter 1: Getting started with angularjsdirective Remarks AngularJS Directives are custom elements in HTML (such as an attribute, element name, comment or CSS class) that tell AngularJS to attach a specified behavior to that DOM element, or even to transform the DOM element and its children. In short, when we create a directive, AngularJS will treat that element differently. Examples Installation or Setup Directives comes with the AngularJS library itself. A sample directive can be created as: angular.module('simpleDirective', []) .directive('helloData', function() { return { template: 'Hello, {{data}}' }; }); And can be used as: JS: angular.module('app', ['simpleDirective']) .controller('Controller', [' scope', function( scope) { scope.data 'World'; }]) HTML div ng-controller "Controller" div hello-data /div /div Will be compiled as: Hello, World Building a reusable component Directives can be used to build reusable components. Here is an example of a "user box" component: https://riptutorial.com/ 2

userBox.js angular.module('simpleDirective', []).directive('userBox', function() { return { scope: { username: ' username', reputation: ' reputation' }, templateUrl: '/path/to/app/directives/user-box.html' }; }); Controller.js var myApp angular.module('myApp', ['simpleDirective']); myApp.controller('Controller', function( scope) { scope.user "John Doe"; scope.rep 1250; }); myPage.js html lang "en" ng-app "myApp" head script src "/path/to/app/angular.min.js" /script script src "/path/to/app/controllers/Controller.js" /script script src "/path/to/app/directives/userBox.js" /script /head body div ng-controller "Controller" user-box username "user" reputation "rep" /user-box /div /body /html user-box.html div {{username}} /div div {{reputation}} reputation /div Your first directive Our first element directive will not do much: it will just calculate 2 2 and will be called in html like this: my-calculator /my-calculator Notice the name of the directive is myCalculator (in CamelCase), but in html it's used as mycalculator (in lisp-case). https://riptutorial.com/ 3

Since we want our directive to be used as html element, we will use restrict: 'E'. Every directive has the template which will be compiled and inserted. Our directive is very simple, so we will insert our html as string into a template parameter. // directives/my-calculator.js angular.module('exampleApp', []) .directive('myCalculator', function() { return { restrict: 'E', template: ' span My directive can calculate 2 2: {{2 2}} /span ' }; }); HTML !DOCTYPE html html ng-app "exampleApp" head script src .5.6/angular.min.js" /script script src "my-calculator.js" /script /head body Here is my first directive: my-calculator /my-calculator /body /html The result will look like this: Here is my first directive: My directive can calculate 2 2: 4 If you want to play with the live example, go to plunkr. Success/Error pop-up message using simple link function Link function is best way in custom directives to manipulate DOM. It takes three attributes as input (scope, element, attribute) in sequence scope: its local scope object of directive. element: html element on which directive is used. attribute: it gives access to all attributes used in element refered. // on success call or similarly error, warning, info in controller scope.message { text: "Saved Successfully", type: "SUCCESS" https://riptutorial.com/ 4

}; user-info msg "message" /user-info //in html var mainApp angular.module("mainApp", []); mainApp.directive('userInfo', function() { var directive {}; directive.restrict 'E'; directive.scope { message : " msg" }, directive.link function(scope, element, attributes) { if(scope.message.type 'SUCCESS') scope.message.text 'SUCCESS: ' scope.message.text ' !'; else if(scope.message.type 'ERROR') scope.message.text 'ERROR: ' scope.message.text ' !'; else if(scope.message.type 'WARNING') scope.message.text 'WARNING: ' scope.message.text ' !' else if(scope.message.type 'INFO') scope.message.text 'INFO: ' scope.message.text ' !' element.on('click', function(event) { //on click of div pop-up will smoothly close (this).fadeOut(); }); }, directive.template ' div ng-class {{message.type}} ' // one can create different bg-color as per type of message and width/height ' div class "message-text" {{message.text}} div ' //message text will be printed ' div '; return directive; }); Read Getting started with angularjs-directive online: 855/getting-started-with-angularjs-directive https://riptutorial.com/ 5

Chapter 2: Commonly Used Directives Examples ngConfirmClick: Confirm before evaluating expression. Description: Evaluate expression after user's confirmation. Arguments: ng-confirm-click:(expression) Expression to evaluate when confirmed. ng-confirm-message:(template) Message to be shown in confirm dialog. Code: Directives.directive("ngConfirmClick", [" parse"," interpolate",function ( parse, interpolate) { return { restrict:"A", priority:-1, compile:function(ele,attr){ var fn parse(attr.ngConfirmClick, null, true); return function ngEventHandler(scope, ele) { ele.on('click', function (event) { var callback function () { fn(scope, { event: "confirm"}); }; var message interpolate(attr.ngConfirmMessage)(scope) 'Are you sure?'; if(confirm(message)) { if (scope. root. phase) { scope. evalAsync(callback); } else { scope. apply(callback); } } }); } } } }]); Working Example Read Commonly Used Directives online: 099/commonly-used-directives https://riptutorial.com/ 6

Credits S. No Chapters Contributors 1 Getting started with angularjs-directive Asim K T, Charlie H, Community, Daniel, ganqqwerty, Tjaart van der Walt, Vishal Singh 2 Commonly Used Directives MMhunter https://riptutorial.com/ 7

AngularJS Directives are custom elements in HTML (such as an attribute, element name, comment or CSS class) that tell AngularJS to attach a specified behavior to that DOM element, or even to transform the DOM element and its children. In short, when we create a directive, AngularJS will treat that element differently. Examples Installation or Setup

Related Documents:

Code Explanation for ng-transclude Directive in AngularJS: 1. The ng-app specifies the root element ( myApp ) to define AngularJS . ng-transclude directive is used to include the existing content "AngularJS" . Sample Output for ng-transclude Directive in AngularJS: 1. The content welcome to wikitechy is displayed in the output using the .

AngularJS uses dependency injection and make use of separation of concerns. AngularJS provides reusable components. AngularJS viii With AngularJS, the developers can achieve more functionality with short code. In AngularJS, views are pure html pages, and controllers written in JavaScript do the business processing. On the top of everything, AngularJS applications can run on all major browsers .

AngularJS Tutorial W3SCHOOLS.com AngularJS extends HTML with new attributes. AngularJS is perfect for Single Page Applications (SPAs). AngularJS is easy to learn. This Tutorial This tutorial is specially designed to help you learn AngularJS as quickly and efficiently as possible. First, you will learn the basics of AngularJS: directives, expressions, filters, modules, and controllers. Then you .

Beginning AngularJS Beginning AngularJS is your step-by-step guide to learning the powerful AngularJS JavaScript framework. AngularJS is one of the most respected and innovative frameworks for building properly structured, easy-to-develop web applications. This book will teach you the absolute essentials, from downloading and installing AngularJS, to using modules, controllers, expressions .

AngularJS provides data binding capability to HTML thus giving user a rich and responsive experience AngularJS code is unit testable. AngularJS uses dependency injection and make use of separation of concerns. AngularJS provides reusable components. With AngularJS,

AngularJS is a JavaScript framework. It is a library written in JavaScript. AngularJS is distributed as a JavaScript file, and can be added to a web page with a script tag: [3] AngularJS extends HTML with ng-directives. The ng-app directive defines that this is an AngularJS application.

AngularJS Tutorial, AngularJS Example pdf, AngularJS, AngularJS Example, angular ajax example, angular filter example, angular controller Created Date 11/29/2015 3:37:05 AM

Japanese teachers of the foreign language as specified by the supervisor and/or principal of the board of education and/or school. The following is a general outline of duties, though they may vary from one Contracting Organisation to another. (1) Assistance in foreign language classes, etc. taught in elementary, junior high and senior high schools. (2) Assistance in foreign language .