Created
January 10, 2014 18:47
-
-
Save pyetras/8360220 to your computer and use it in GitHub Desktop.
ng-if for older (1.0.X) angularjs versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
}); | |
}; | |
} | |
}; | |
}]); |
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
Sorry but how to call ng-if in html??
Thanks.