How to prevent duplicated attributes in angular directive when replace=true

前端 未结 2 1947
半阙折子戏
半阙折子戏 2021-01-02 10:59

I\'ve found that angular directives that specify replace: true will copy attributes from the directive usage into the output rendered by the template. If the te

2条回答
  •  无人及你
    2021-01-02 11:13

    It would be helpful to know how you expect the values to be merged. Does the template take priority, the element, or is some kind of merge needed?

    Lacking that I can only make an assumption, the below code assumes you want to remove attributes from the template that exist on the element.

    .directive('foo', function() {
        return {
            restrict: 'E',
            replace: true,
            template: function(element, attrs) {
                var template = '
    '; template = angular.element(template); Object.keys(attrs.$attr).forEach(function(attr) {\ // Remove all attributes on the element from the template before returning it. template.removeAttr(attrs.$attr[attr]); }); return template; }, scope: { bar: '@' } }; })

提交回复
热议问题