相同的打字稿,但在Angular 8中使用了不同的HTML视图格式

问题描述 投票:0回答:2

我有一个打字稿表格,该表格收集Formbuilder数据,并对其应用一些逻辑。客户提出了Requirement,要求附加一个不同的HTML视图以使用该逻辑。 Angular中是否有可选项,以保持复制相同的Typescript,但使用不同的HTML?]

说这是在里面

app-mailing-address-ts。

想用其他东西代替html,

  public electronicsFormChangeEvent(electronicsFormEvent) {
    this.productForm = electronicsProductEvent;
  }

  public furnitureFormChangeEvent(furnitureFormEvent) {
    this.productForm = furnitureProductEvent;
  }

  public groceryFormChangeEvent(groceryFormEvent) {
    this.productForm = groceryFormEvent;
  }


  public bookFormChangeEvent(bookFormEvent) {
    this.productForm = bookFormEvent;
  }

  public carFormChangeEvent(carFormEvent) {
    this.productForm = poBoxFormEvent;
  }


  public calculateTotalAmount() {
    if (this.productForm == 'Furniture') {
        this.TotalAmount = UnitAmount * Quantity
        ......
        .....
    }
    this.calculateTotal = true;
  }
angular typescript angular8
2个回答
0
投票

是的,在Angular中,您有不同的方式共享代码。

pipesconstants,最常见的是services。您想要将包含通用代码的共享服务注入到两个不同的组件中,这些组件将定义不同的HTML模板。


0
投票

您可以为通用代码创建Singleton服务。https://angular.io/guide/singleton-servicesHow to create a common service throughout the app in angular4

如果需要创建可在“模板驱动表单”和“反应性表单”中使用的共享组件。这是一个很好的例子https://codeburst.io/how-to-create-shared-components-in-angular-template-driven-and-reactive-form-ddb095cc7904

© www.soinside.com 2019 - 2024. All rights reserved.