【问题标题】:Attribute directives angular属性指令角度
【发布时间】:2020-03-10 04:16:50
【问题描述】:

我创建了一个属性指令来根据输入的一系列数字显示网络徽标。但是,我无法让我的 HTML 页面显示任何内容。我的项目编译,但 HTML 页面显示为空白。我做错了什么?

下面是我的 app.component.html 代码:

<input type="text"[(ngModel)]="cardNumberInput"/>
<p>{{cardNumberInput}}</p>
<img applogo [cardType]="cardNumberInput" width="30"/>

下面是我的 app.module.ts 代码:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { FormsModule } from '@angular/forms'
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LogoDirective } from './logo.directive';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    LogoDirective
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

directive.ts:

import {Directive, Input, OnChanges, HostBinding} from '@angular/core';

enum CardType { VISA = 'visa', MASTERCARD = 'mastercard', AMERICAN_EXPRESS = 'aexpress', DISCOVER = 'discover', DINERS = 'diners', UNKNOWN = 'unknown'}

@Directive({
  selector: '[logo]'
})
export class LogoDirective implements OnChanges {


  @HostBinding('src')
  imageSource;

  @Input()
  cardNumber: string;

  ngOnChanges() {
    this.imageSource = 'assets/cards' + this.getCardTypeFromNumber() + '.png';
  }

  getCardTypeFromNumber(): CardType {
    if (this.cardNumber) {
      if (this.cardNumber.startsWith('34')) {
        return CardType.AMERICAN_EXPRESS;
      } else if (this.cardNumber.startsWith('4')) {
        return CardType.VISA;
      } else if (this.cardNumber.startsWith('5')) {
        return CardType.MASTERCARD;
      } else if (this.cardNumber.startsWith('60')) {
        return CardType.DISCOVER;
      } else if (this.cardNumber.startsWith('30')) {
        return CardType.DINERS;
      }
    }
    return CardType.UNKNOWN;
  }

}

directive.spec.ts:

import { LogoDirective } from './logo.directive';

describe('LogoDirective', () => {
  it('should create an instance', () => {
    const directive = new LogoDirective();
    expect(directive).toBeTruthy();
  });
});

【问题讨论】:

    标签: javascript angular angular-directive


    【解决方案1】:

    您的属性绑定有几个错误。请纠正您的模板如下:

    1. 您的属性选择器名称是 logo 而不是 applogo
    2. 您的属性输入名称是 cardNumber 而不是 cardType
    3. 您没有在图像中指定 src 属性。

    需要更改

      constructor(private element: ElementRef){
      }
    
      ngOnChanges() {
        debugger;
        this.imageSource = 'assets/cards' + this.getCardTypeFromNumber() + '.png';
        this.element.nativeElement.src = this.imageSource;
        this.element.nativeElement.alt = this.imageSource;
      }
    
    <input type="text"[(ngModel)]="cardNumberInput"/>
    <p>{{cardNumberInput}}</p>
    <img logo [cardNumber]="cardNumberInput" width="30"  />
    

    logo.directive.ts

    import {Directive, Input, OnChanges, HostBinding, ElementRef} from '@angular/core';
    
    enum CardType { VISA = 'visa', MASTERCARD = 'mastercard', AMERICAN_EXPRESS = 'aexpress', DISCOVER = 'discover', DINERS = 'diners', UNKNOWN = 'unknown'}
    
    @Directive({
      selector: '[logo]'
    })
    export class LogoDirective implements OnChanges {
    imageSource:string ='';
    
      @Input()
      cardNumber: string;
      constructor(private element: ElementRef){
      }
    
      ngOnChanges() {
        debugger;
        this.imageSource = 'assets/cards' + this.getCardTypeFromNumber() + '.png';
        this.element.nativeElement.src = this.imageSource;
        this.element.nativeElement.alt = this.imageSource;
      }
    
      getCardTypeFromNumber(): CardType {
        if (this.cardNumber) {
          if (this.cardNumber.startsWith('34')) {
            return CardType.AMERICAN_EXPRESS;
          } else if (this.cardNumber.startsWith('4')) {
            return CardType.VISA;
          } else if (this.cardNumber.startsWith('5')) {
            return CardType.MASTERCARD;
          } else if (this.cardNumber.startsWith('60')) {
            return CardType.DISCOVER;
          } else if (this.cardNumber.startsWith('30')) {
            return CardType.DINERS;
          }
        }
        return CardType.UNKNOWN;
      }
    }
    
    <input type="text"[(ngModel)]="cardNumberInput"/>
    <p>{{cardNumberInput}}</p>
    <img logo [cardNumber]="cardNumberInput" width="30"  />
    

    你可以看到这个正在工作的 stackblitz - https://stackblitz.com/edit/angular-4bd23d 它显示了 alt 属性,因为我没有图像。

    我希望它会有所帮助。

    【讨论】:

    • 嗨王子,感谢您的回复。我不确定如何将我的项目上传到 StackBlitz。更正后我仍然遇到问题。
    • 给我一些时间,我会为你添加解决方案。
    • 我也用 stackblitz 链接更新了我的答案。
    • 嘿王子,谢谢!我可以看到它在 Stackblitz 上工作,它在 VSstudio 中对我来说仍然显示为空白,所以我不确定发生了什么。但我确信你的语法是正确的,如果它适用于 Stackblitz。
    • 您能否告诉我您对代码所做的具体更改,以便我知道在哪里查看?
    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 2015-03-03
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多