Creating an Attribute Directive

Let's start with a simple button that moves a user to a different page.

  1. @Component({
  2. selector: 'app-visit-rangle',
  3. template: `
  4. <button
  5. type="button"
  6. (click)="visitRangle()">
  7. Visit Rangle
  8. </button>
  9. `
  10. })
  11. export class VisitRangleComponent {
  12. visitRangle() {
  13. location.href = 'https://rangle.io';
  14. }
  15. }

View Example

We're polite, so rather than just sending the user to a new page, we're going to ask if they're ok with that first by creating an attribute directive and attaching that to the button.

  1. @Directive({
  2. selector: `[appConfirm]`
  3. })
  4. export class ConfirmDirective {
  5. @HostListener('click', ['$event'])
  6. confirmFirst(event: Event) {
  7. return window.confirm('Are you sure you want to do this?');
  8. }
  9. }

View Example

Directives are created by using the @Directive decorator on a class and specifying a selector. For directives, the selector name must be camelCase and wrapped in square brackets to specify that it is an attribute binding. We're using the @HostListener decorator to listen in on events on the component or element it's attached to. In this case we're watching the click event and passing in the event details which are given by the special $event keyword. Next, we want to attach this directive to the button we created earlier.

  1. template: `
  2. <button
  3. type="button"
  4. (click)="visitRangle()"
  5. appConfirm>
  6. Visit Rangle
  7. </button>
  8. `

View Example

Notice, however, that the button doesn't work quite as expected. That's because while we're listening to the click event and showing a confirm dialog, the component's click handler runs before the directive's click handler and there's no communication between the two. To do this we'll need to rewrite our directive to work with the component's click handler.

  1. @Directive({
  2. selector: `[appConfirm]`
  3. })
  4. export class ConfirmDirective {
  5. @Input() appConfirm = () => {};
  6. @HostListener('click', ['$event'])
  7. confirmFirst() {
  8. const confirmed = window.confirm('Are you sure you want to do this?');
  9. if(confirmed) {
  10. this.appConfirm();
  11. }
  12. }
  13. }

View Example

Here, we want to specify what action needs to happen after a confirm dialog's been sent out and to do this we create an input binding just like we would on a component. We'll use our directive name for this binding and our component code changes like this:

  1. <button
  2. type="button"
  3. [appConfirm]="visitRangle">
  4. Visit Rangle
  5. </button>

View Example

Now our button works just as we expected. We might want to be able to customize the message of the confirm dialog however. To do this we'll use another binding.

  1. @Directive({
  2. selector: `[appConfirm]`
  3. })
  4. export class ConfirmDirective {
  5. @Input() appConfirm = () => {};
  6. @Input() confirmMessage = 'Are you sure you want to do this?';
  7. @HostListener('click', ['$event'])
  8. confirmFirst() {
  9. const confirmed = window.confirm(this.confirmMessage);
  10. if(confirmed) {
  11. this.appConfirm();
  12. }
  13. }
  14. }

View Example

Our directive gets a new input property that represents the confirm dialog message, which we pass in to window.confirm call. To take advantage of this new input property, we add another binding to our button.

  1. <button
  2. type="button"
  3. [appConfirm]="visitRangle"
  4. confirmMessage="Click ok to visit Rangle.io!">
  5. Visit Rangle
  6. </button>

View Example

Now we have a button with a customizable confirm message before it moves you to a new url.

原文: https://angular-2-training-book.rangle.io/handout/advanced-angular/directives/creating_an_attribute_directive.html