Skip to content

Instantly share code, notes, and snippets.

@pyetras
Created January 10, 2014 18:47
Show Gist options
  • Save pyetras/8360220 to your computer and use it in GitHub Desktop.
Save pyetras/8360220 to your computer and use it in GitHub Desktop.
ng-if for older (1.0.X) angularjs versions
angular.module('future', []).directive('ngIf', [function() {
return {
transclude: 'element',
priority: 600,
terminal: true,
restrict: 'A',
$$tlb: true,
compile: function ($element, $attr, $transclude) {
return function link($scope, $element, $attr, ctrl){
var block, childScope;
$scope.$watch($attr.ngIf, function ngIfWatchAction(value) {
if (!!value) {
if (!childScope) {
childScope = $scope.$new();
$transclude(childScope, function (clone) {
clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
block = {
clone: clone
};
angular.element($element).after(clone);
});
}
} else {
if (childScope) {
childScope.$destroy();
childScope = null;
}
if (block) {
angular.element(block.clone).remove();
block = null;
}
}
});
};
}
};
}]);
@asantrac
Copy link

Sorry but how to call ng-if in html??

Thanks.

@workguy66
Copy link

very much appreciated, thank you

you can also wrap it into

if(angular.version.major === 1 && angular.version.minor === 0) {
   ... rest of the code ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment