Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 当replace=true时,禁止复制属性_Angularjs - Fatal编程技术网

Angularjs 当replace=true时,禁止复制属性

Angularjs 当replace=true时,禁止复制属性,angularjs,Angularjs,以下指令: var app = angular.module('demo', []); app.directive('myDirective', function() { return { restrict: 'E', template: '<h1>Foo bar</h1>' }; }); .directive('myDirective', function() { return {

以下指令:

var app = angular.module('demo', []);
app.directive('myDirective', function() {
    return {
        restrict: 'E',
        template: '<h1>Foo bar</h1>'
    };
});
    .directive('myDirective', function() {
        return {
            restrict: 'E',
            replace: true,
            template: '<h1>Foo bar</h1>',
            link: function(scope, elm, attrs){
                elm.removeAttr('foo');
            }
        };
    });

请注意,Angular将我的指令的属性复制到模板元素(即
foo=“bar”
)。如何防止这种行为?

您可以手动删除指令链接功能中的属性:

var app = angular.module('demo', []);
app.directive('myDirective', function() {
    return {
        restrict: 'E',
        template: '<h1>Foo bar</h1>'
    };
});
    .directive('myDirective', function() {
        return {
            restrict: 'E',
            replace: true,
            template: '<h1>Foo bar</h1>',
            link: function(scope, elm, attrs){
                elm.removeAttr('foo');
            }
        };
    });
.directive('myDirective',function(){
返回{
限制:'E',
替换:正确,
模板:“Foo-bar”,
链接:功能(范围、elm、属性){
榆树removeAttr(‘foo’);
}
};
});
在你的情况下,这个指令是有效的

编辑:您可以通过一个简单的循环将其扩展为动态删除所有属性:

    .directive('myDirective', function() {
        return {
            restrict: 'E',
            replace: true,
            template: '<h1>Foo bar</h1>',
            link: function(scope, elm, attrs){
                for(var attr in attrs.$attr){
                    elm.removeAttr(attr);
                }
            }
        };
    });
.directive('myDirective',function(){
返回{
限制:'E',
替换:正确,
模板:“Foo-bar”,
链接:功能(范围、elm、属性){
for(属性中的var attr.$attr){
榆树(attr);
}
}
};
});

这不是一种删除属性的动态方法,是否有一种通用方法可以在详细说明指令的所有属性后删除它们?我理解你的问题,但对否决票感到失望。我的答案适用于所问的问题,扩展它以删除所有属性非常简单。见编辑,谢谢。不过,在大多数情况下,最好发布一个正确的答案:)
    .directive('myDirective', function() {
        return {
            restrict: 'E',
            replace: true,
            template: '<h1>Foo bar</h1>',
            link: function(scope, elm, attrs){
                for(var attr in attrs.$attr){
                    elm.removeAttr(attr);
                }
            }
        };
    });