AngularJS 1.XX - Why does anyone use the "M" constraint for a directive?

I know that the angular directive can be defined in four ways:

'A' - only matches attribute name
'E' - only matches element name
'C' - only matches class name
'M' - only matches comment

For example, a directive with the expression "M":

angular.module('exampleApp', [])
   .directive('myDirective', function() {
      return {
         restrict: 'M',
         ...
      };
});

and declaring a directive in HTML

<!-- directive: my-directive -->

But why would anyone use a constraint Mfor a directive? I find it really strange. Because if I comment on the code, I do not want it to run. So why is this the case?

+4
source share
2 answers

From docs :

: . , .

: , DOM API , (, ). AngularJS 1.2 ng-repeat-start ng-repeat-end . .

, .

post .

, ,

(function() {
  angular
    .module('exampleApp', [])
    .directive("comment", function() {
      return {
        restrict: 'M',
        replace : true,
        template : "<h1>Made by a comment directive!</h1>",
        link: function(scope, element, attrs) {
          console.log(attrs.comment);
        }
      };
    })
})();
<!DOCTYPE html>
<html ng-app='exampleApp'>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body>

</body>

<body>
  <!-- directive: comment comment argument string -->
</body>

</html>
Hide result
+3

, , , .

HTML, , Angular /. / DOM node. HTML- , . Angular . , , , DOM. directive: ... ; , , , .

+3

Source: https://habr.com/ru/post/1655170/


All Articles