Skip to content

Instantly share code, notes, and snippets.

@pmanijak
Last active April 24, 2016 20:42
Show Gist options
  • Save pmanijak/2c78432000aa6c15c9792acc195f6af5 to your computer and use it in GitHub Desktop.
Save pmanijak/2c78432000aa6c15c9792acc195f6af5 to your computer and use it in GitHub Desktop.
Angular directive that works with other directives
// A basic template for Angular directives
// that work with other directives.
'use strict';
angular.module('ModuleName.directives')
.directive('myDirective', ['$timeout', 'serviceDependency',
function ($timeout, serviceDependency) {
// Directives accept dependencies in the typical
// Angular way. Be aware that you cannot put
// $scope or $element above -- they belong either
// as dependencies for the controller or parameters
// in the link function.
var controller = ['$scope', function ($scope) {
// The 'controller' function is for exposing
// our API to other directives.
return {
doThing: function () {
// If other directives depend on this
// controller, they can write
// myDirective.doThing() to call
// this function.
}
}
}];
var link = function (scope, element, attr, controllerDependency) {
// This is called after 'controller' above. It is used to
// receive and bind to the required 'controllerDependency'.
//
// The bulk of code is typically here.
//
// 'controllerDependency' is an instance of
// 'parentControllerDependency' that is declared
// in the template somewhere.
};
return {
controller: controller,
link: link,
// Prefix options:
// ^^ Look for 'parentControllerDependency' parent element
// ^ Look for parent element or peer-level attribute
// Look for peer-level attribute
require: '^parentControllerDependency'
}
}]);
'use strict';
angular.module('CircleBlvd.directives')
.directive('cbStoryListBuilder', ['$timeout', 'errors',
function ($timeout, errors) {
var controller = ['$scope', function ($scope) {
}];
var link = function (scope, element, attr, storyListCtrl) {
};
return {
controller: controller,
link: link,
require: '^spStoryList'
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment