On Github mo-gr / bedcon-angular-directives
Berlin native. Bit herder. Code connoisseur. Coffee lover.
Freelance Software Entwickler & Trainer
Web – JVM – iOS
ng-app, ng-controller, ng-click, ...
Nicht nur Attribute, auch Elemente
$("#content form.form ul ~ input + div").on("click", fmlHandler);
vs.
You can't touch this!
var module = angular.module('Example', []); module.directive('directiveName', function() { return { // describe directive }; });
app.directive('fooBar', ...)
<foo-bar/> <foo:bar/> <foo_bar/>
<my-user/>Attribut
<div my-user='user'>CSS Klasse
<div class='my-user: user;'>Kommentar
<!-- directive: my-user user -->
var module = angular.module('Example', []); module.directive('directiveName', function() { return { // describe directive link: linkFunction, restrict: 'EA' //...etc }; });
Link-Funktion
nimmt nicht am DI teil
link: function (scope, element, attribute) { // element is jqLite/jQuery wrapped // code to run on/with that element }
Wo kann die directive verwendet werden
String aus den Zeichen:
{ // snip restrict: 'EA', // ...snip }
neue DOM Elemente in die Direktiven einfügen
entweder inline oder über Angulars templateCache
app.directive('placeholder', function () { return { restrict: 'E', template: '
neues template in DOM Element einfügen oder ersetzen
default ist false
app.directive('placeholder', function () { return { restrict: 'E', replace: true, template: '
var sound = new Howl({ urls: ['path/to/sound.mp3'] }); sound.play();
Wir brauchen:
Test Boilerplate – Jasmine Test Runner – Karma Angular Boilerplate – Angular Mocksapp.directive('noise', function($window) { return { link: function (scope, element, attrs) { var sound = new $window.Howl({ urls: [attrs.noise] }); element.on('click', function() {sound.play();}); } }; })
describe('MyDirective Test', function () { var scope, element; // Load module beforeEach(module('MyModule')); // prepare directive beforeEach(inject(function($compile, $rootScope) { scope = $rootScope.$new(); element = angular.element('
app.directive('noise', function($window) { return { link: function (scope, element, attrs) { var sound = new $window.Howl({ urls: [attrs.noise], onplay: function () { scope.$apply(function () { scope.playing = attrs.noise; }); } }); element.on('click', function() {sound.play();}); } }; })
app.directive('complexDirective', function () { return { controller: 'complexDirectiveController', link: function(scope, elem, attr, controller) { controller.init(elem); }; }; }); app.controller('complexDirectiveController', function($scope, ComplexService) { // magic happens here });
app.directive('fooBar', function() { return { controller: 'fooBarController', require: ['reqDir', '?^optDir', 'fooBar'], link: function(s, e, a, controllers) { var reqDir = controllers[0]; var ownController = controllers[2]; if (controllers[1]) controllers[1].interact(reqDir); }; } });
app.directive('messageBox', function() { return { transclude: true, template: '
Something went wrong: {{message}}
ermöglicht definiertes Interface einer Direktive
'@' – unidirektionales Mapping in den Direktiven Scope '=' – bidirektionales Mapping zwischen umliegendendem Scope und Direktive '&' – ermöglicht function calls im umliegendem Scopeapp.directive('example', function(MagicService) { // lazy one-time init return { restrict: 'EA', link: function(scope, elem, attrs, controllers) { // called per usage }, // or compile: compileFn, templateUrl: 'neatTemplate.html', ,// or template: '...' replace: true, transclude: true, scope: {foo:'='}, controller: 'aController', require: ['?^other', 'example'] priority: 100, terminal: false }; });
Get in touch!