Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of restrict: AE in Angular2

In Angular 1, you could use restrict to determine how your directive could be used. For example:

angular.module('foo').directive('fooDirective', [function () {
  return {
    restrict: 'AE',
    /* ... */
  }
}]);

And then you would be able to use this in your template either as an attribute or as an element:

<foo />

OR

<div foo />

I've been reading on sites such as this one that in Angular2, the select proper way to define how your component is used now:

@Component({
  selector: 'Sample'
});

Then in your template:

<Sample />

Similarly, adding brackets allows it to be used as an attribute:

@Component({
  selector: '[Sample]'
});

Template:

<div Sample />

But what if I want to allow it to be used both as an attribute and as an element, the equivalent of restrict: 'AE' in Angular 1?

like image 751
A. Duff Avatar asked Nov 25 '25 22:11

A. Duff


1 Answers

You can use

@Component({
  selector: 'sample,[Sample]'
});

See also https://github.com/angular/angular/blob/60727c4d2ba1e4b0b9455c767d0ef152bcedc7c2/modules/angular2/src/common/forms/directives/ng_form.ts#L81 for an example

@Directive({ selector: 'form:not([ngNoForm]):not([ngFormModel]),ngForm,[ngForm]',

like image 164
Günter Zöchbauer Avatar answered Nov 28 '25 13:11

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!