AngularJS
Custom Directives
AngularJS Custom Directives Recall: Angular Directives Original - - PowerPoint PPT Presentation
AngularJS Custom Directives Recall: Angular Directives Original Definition: a marker on an html tag that tells our HTML when to trigger or run our JavaScript HTML attributes with an ng- prefix used by AngularJS to extend HTML E.g.,
Custom Directives
to extend HTML
JavaScript behaviors
would need to be de-cluttered.
HTML snippet, e.g., product title and price.
needed
<h3 ng-include=”’product-title.html’” novalidate> </h3>
scheme -- file:///
error
manages your dependencies
name, description, and entry point (server.js).
package.json manifest
as the code changes
1 // We need an express application to run a webserver 2 var express = require('express'); 3 var path = require('path'); 4 var expressApp = express(); 5 6 expressApp.set("port", process.env.PORT || 3000); 7 8 // Express middleware 9 expressApp 10 .use(express.static('./')) 11 .get('*', function(req, res){ 12 res.sendFile('index.html', {root: path.join(__dirname, './')}); 13 }) 14 .listen(expressApp.get('port'), function(){ 15 console.log('Server is listening on port ' + expressApp.get('port')); 16 });
> nodemon server.js
loaded changes
the server sends back the index.html file and the assets for that page
is encountered, another request is issued to the server for that file and the HTML is returned
behavior of your application
can tell what it does
directives!!
1 var app = angular.module('store', []); ... 27 app.directive('productTitle', function(){ 28 // return a configuratin object that defines 29 // how the directive will work. 30 return { 31 restrict: 'E', // Type of directive (E for HTML Element) 32 templateUrl: 'views/product-title.html’ 33 // URL to template to load into the page 34 }; 35 });
HTML will translate to camelCase in your JavaScript
need
the HTML into the page
<product-title></product-title>
closing tags. Some browsers do not like self-closing tags for custom elements.
an attribute of an HTML element.
and Attribute Directives for mixin behaviors like tooltip.
you think you'll be able to understand the functionality just by looking at the HTML?
should be able to understand the behavior and intent from just the HTML
expressive
product-specs.html template
productSpecs to load it
a separate template file, e.g., product-panels.html
functionality into that directive using the controller attribute
the controller
template code was extracted
directive with a controller
directive (no controller)