<div ng-show="showThis">...</div>
directive('ngShow', ['$animate', function($animate) {
return function(scope, element, attr) {
scope.$watch(attr.ngShow, function(value){
$animate[toBoolean(value) ? 'removeClass' : 'addClass'](element,
'ng-hide');
});
};
}]);
Usage:
<div ng-class="myClass()">...</div>
Code: (modified slightly for brevity and clarity)
directive('ngClass', function() {
return {
restrict: 'A',
link: function(scope, element, attr) {
var oldVal;
scope.$watch(attr.ngClass, ngClassWatchAction, true);
attr.$observe('class', function(value) {
ngClassWatchAction(scope.$eval(attr.ngClass));
});
function ngClassWatchAction(newVal) {
if (selector === true || scope.$index % 2 === selector) {
var newClasses = flattenClasses(newVal || '');
if(!oldVal) {
attr.$addClass(newClasses);
} else if(!equals(newVal,oldVal)) {
attr.$updateClass(newClasses, flattenClasses(oldVal));
}
}
oldVal = copy(newVal);
}
function flattenClasses(classVal) {
// snipped for brevity
}
}
};
});
<my-widget>
<div ng-click="...">
<div ui-date></div>
angular.module('MyDirectives', []);
angular.module('MyApp', ['MyDirectives']);
angular.module('MyDirectives').
directive('myDirective', function() {
// TODO Make this do something
});
<div my-directive>...</div>
<input type="text" select-all-on-focus />
angular.module('MyDirectives').
directive('selectAllOnFocus', function() {
return {
restrict: 'A', // more on this soon
link: function(scope, element) {
element.mouseup(function(event) {
event.preventDefault();
});
element.focus(function() {
element.select();
});
}}});
<input type="text" ng-model="name" select-all-on-focus />
<input type="text" ng-model="age" select-all-on-focus />
restrict: 'A'
<div my-directive></div>
restrict: 'E'
<my-directive></my-directive>
restrict: 'AE'
<my-social-buttons></my-social-buttons>
<div>
<a href="x"><i class="fa fa-facebook"></i></a>
<a href="y"><i class="fa fa-twitter"></i></a>
<a href="z"><i class="fa fa-google-plus"></i></a>
</div>
directive('mySocialButtons', function() {
return {
restrict: 'E',
template:
'<div>' +
' <a href="x"><i class="fa fa-facebook"></i></a>' +
' <a href="y"><i class="fa fa-twitter"></i></a>' +
' <a href="z"><i class="fa fa-google-plus"></i></a>' +
'</div>'
}
});
<script type="text/ng-template"
id="/partials/my-social-buttons.html">
<div>
<a href="x"><i class="fa fa-facebook"></i></a>
<a href="y"><i class="fa fa-twitter"></i></a>
<a href="z"><i class="fa fa-google-plus"></i></a>
</div>
</script>
$http.get(templateUrl, {cache: $templateCache})
<my-searchbox
search-text="theSearchText"
placeholder="Please search now"
></my-searchbox>
angular.module('MyDirectives').
directive('mySearchbox', function() {
return {
restrict: 'E',
scope: {
searchText: '=',
placeholder: '@',
usedLucky: '='
},
template:
'<div>' +
' <input type="text" ng-model="tempSearchText" />' +
' <button ng-click="searchClicked()">' +
' Search' +
' </button>' +
' <button ng-click="luckyClicked()">' +
' I\'m feeling lucky' +
' </button>' +
'</div>'
}
});
scope: {
someParameter: '='
}
scope: true
scope: {
param1: '=', // two-way binding (reference)
param2: '@', // one-way expression (top down)
param3: '&' // one-way behavior (bottom up)
}
<div my-directive
param1="someVariable"
param2="My name is {{theName}}, and I am {{theAge+10}}."
param3="doSomething(theName)"
>
directive('mySearchbox', function() {
return {
restrict: 'E',
scope: {
searchText: '=',
placeholder: '@',
usedLucky: '='
},
template: /* snip */,
link: function(scope, element, attrs) {
scope.searchClicked = function() {
scope.searchText = scope.tempSearchText;
scope.usedLucky = false;
}
scope.luckyClicked = function() {
scope.searchText = scope.tempSearchText;
scope.usedLucky = true;
}
}
}
});
directive('myDirective', function() {
return {
link: function (scope, element, attrs) {
var options = scope.$eval(attrs.myDirective)
function MyController($scope) {
$scope.someVariable = 42
<div my-directive="{foo: 1, bar: someVariable}">
{foo: 1, bar: 42}
var template = Handlebars.compile('<div>...</div>');
var html = template({foo: 1, bar: 2});
<div my-lazy-load="readyToShow">
<!-- Expensive DOM Here -->
</div>
directive('myLazyLoad', function() {
return {
transclude: 'element',
priority: 1200, // changed needed for 1.2
terminal: true,
restrict: 'A',
compile: function(element, attr, linker) {
return function (scope, iterStartElement, attr) {
var hasBeenShown = false;
var unwatchFn = scope.$watch(attr.myLazyLoad, function(value){
if (value && !hasBeenShown) {
hasBeenShown = true;
linker(scope, function (clone) {
iterStartElement.after(clone);
});
unwatchFn();
}
});
}
}
});
directive('myDialog', function() {
return { restrict: 'E', transclude: true, template: '<div class="modal">' + ' <div class="modal-body" ng-transclude></div>' + '</div>' } });
<my-dialog>
<h1>Some Custom Title</h1>
<p>Some custom dialog content goes here</p>
</my-dialog>
<div class="modal">
<div class="modal-body">
<h1>Some Custom Title</h1>
<p>Some custom dialog content goes here</p>
</div>
</div>