首页 > 解决方案 > 链接两个类之间的变量以输入计算值

问题描述

在我的 Spring + Angular 项目中,我必须插入一个摘要页面。在上一页 ( codeChoice.ts ) 上,用户可以选择一个code. 此代码的值在摘要页面上用于执行计算。


    code: CodeA[] = []
    
    //...
    
    goToSummary() {
        this.router.navigate(['/summary'], {
          state: {
            code: this.code
          }
        })
    }

在摘要页面中,我必须将我拥有的 CodeA 的价格添加到相关的 CodeB 中,然后对这些价格应用百分比和附件的字母参数。这就是为什么我想在后端创建一个关联类来管理两个类之间的联合:


    @Entity
    @Table(name = "associationSummary")
    public class AssociationSummary {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long id;
    
        @ManyToOne(cascade = CascadeType.ALL)
        private CodeA codeA;
    
        @ManyToOne(cascade = CascadeType.ALL)
        private CodeB codeB;
    
        @Column
        private int percentage;
        private string accessories;
    
        // GET AND SET

}

相应地在前端:


    export class AssociationSummary {
        id: number
        codeA: CodeA
        codeB: CodeB
        percentage: number
        accessories: string
    }

其中 CodeA 和 CodeB 有这个变量:


    export class CodeA {
    id: number
    codeA: string
    priceA: number
    }
    
    export class CodeB {
    id: number
    codeB: string
    priceB: number
    }

因此,有了这些数据,我如何将用户在 prevoius 页面中选择的代码链接CodeA到相关的类AssociationSummary?在summary.ts页面中,我现在已经这样做了:


    export class Summary implements OnInit {
    
      constructor(
        private service: AssociationSummaryService
      ) { }
      
      code: CodeA[]
      association: AssociationSummary[] 
      
      ngOnInit() {
        //I load the service data from the db
        this.service.getAssociationSummary().subscribe(data => {
          this.association = data;
        })
        // the code taken from the previous page
        this.code = window.history.state.code
    }
    }

这是摘要页面的示例:

CODE A: ...
CODE B: ... (select of codeB if there are more codeB associated at CodeA)
------------
codeA price: ...
codeB price: ...
------------
Sum: ...
Sum * percentage: ...

标签: javaarraysangularspringtypescript

解决方案


推荐阅读