AngularJS有限制使用ng-if仅限于attribute由A如下所示。所以,你不能在comment
var ngIfDirective = ['$animate', function($animate) {
return {
transclude: 'element',
priority: 600,
terminal: true,
restrict: 'A',
使用此相反,你可以创建的是定制指令与如下限制M。我在下面写的当前指令可用于每个元素的一条评论。如果您需要处理多个元素,则应该扩展指令。
注意:我用下面只有div显示/隐藏价值link没有任何显示
var app = angular.module('app', []);
app.controller('TestController', function($scope){
$scope.checked = true;
});
app.directive('ngIfExt', function() {
return {
restrict: 'M',
link: function(scope, element, attrs) {
scope.$watch(attrs.ngIfExt, function(value, oldValue) {
if(value) {
if(attrs.target){
$(element).after(attrs.target);
}
} else {
var targetElement = $(element).next().clone();
attrs.target = targetElement;
$(element).next().remove();
}
}, true);
}};
});
angular.bootstrap(document, ['app']);
Show/hide