How to replace the element with ng-transclude

后端 未结 4 1165
名媛妹妹
名媛妹妹 2021-01-30 17:24

Is it possible to replace the element with ng-transclude on it rather than the entire template element?

HTML:

4条回答
  •  暖寄归人
    2021-01-30 17:31

    It's easy to create a ng-transclude-replace directive, here is a copycat of the original ng-transclude.

    directive('ngTranscludeReplace', ['$log', function ($log) {
                  return {
                      terminal: true,
                      restrict: 'EA',
    
                      link: function ($scope, $element, $attr, ctrl, transclude) {
                          if (!transclude) {
                              $log.error('orphan',
                                         'Illegal use of ngTranscludeReplace directive in the template! ' +
                                         'No parent directive that requires a transclusion found. ');
                              return;
                          }
                          transclude(function (clone) {
                              if (clone.length) {
                                  $element.replaceWith(clone);
                              }
                              else {
                                  $element.remove();
                              }
                          });
                      }
                  };
              }]);
    

    PS:you can also check this link to see the difference between the ng-transclude

提交回复
热议问题