角度7:组件中更改的对象未反映在视图中

时间:2019-06-01 17:49:01

标签: angular angular-changedetection

我已经在angular 7应用程序中实现了下拉控件,并且在值更改时,我正在过滤集合并将其分配给绑定到视图的对象。我正在筛选的收集对象称为FundTerms,绑定到视图的我分配的对象称为Fund。我已经在编写过滤逻辑的组件中实现了值更改事件。

我可以看到valueChanged事件被触发并执行了逻辑,但是UI没有在VehicleType字段中反映出更改后的值。

我在视图中有两个控件。一个是下拉控件,即“名称”,另一个是VehicleType字段,该字段根据“名称”控件的下拉菜单中选择的内容显示VehicleType

组件

import { Component, OnInit, AfterViewInit,AfterViewChecked, AfterContentInit, Input, ViewChild, Output, EventEmitter } from '@angular/core';
import { TermsService } from '../../services/terms.service';
import { NotifyService } from '../../utilities/notify.service';



@Component({
    selector: 'app-fund-terms',
    templateUrl: './fundTerms.component.html'
})

export class FundTermsComponent implements OnInit {


    @Input() Funds: any;
    public Fund: any;
    _fundTerms: any;

    get FundTerms(): any {
        return this._fundTerms;
    }

     @Input('FundTerms')
    set FundTerms(value: any) {
        this._fundTerms = value;
        if (this._fundTerms) {
        this.Fund = this._fundTerms[0];
        }
    }

    public EditMode = false;

    ngOnInit() {
    }


    public fundChanged(value: any): void {
       this.Fund  =  this.FundTerms.filter(x => x.Id === value.Id);
    }
}

查看

<div *ngIf="Fund">
        <div class="card-body">

            <div class="form-group row" style="width: 100%;">
                <div class="col-md-4">
                    <label for="inputName" class="col-form-label modal-label">Name</label>
                    <div *ngIf="!EditMode">{{Fund.Name}}</div>
                    <kendo-dropdownlist *ngIf="EditMode" style="width:100%" [(ngModel)]="FundClass"
                        class="form-control  form-control-sm" [data]="Funds" [filterable]="false" textField="Name"
                        [valuePrimitive]="false" valueField="Id" (valueChange)="fundChanged($event)">
                    </kendo-dropdownlist>
                </div>


                 <div class="col-md-4">
                    <label for="inputTitle" class="col-md-2  col-form-label ">Vehicle Type</label>
                    <div>{{Fund.VehicleTypeName}}</div>
                </div>
            </div>
        </div>  
    </div>

JSON

[
{
    "Id": 5508,
    "Name": "Sylebra Capital Partners (Offshore) Ltd",
    "VehicleTypeId": 5,
    "VehicleTypeName": "Offshore Fund",
    "InvestmentManager": null,
    "NavReportingCycleId": null,
    "NavReportingCycleName": "",
    "CurrencyId": null,
    "CurrencyName": "",
    "SideLetterAgreement": false,
    "SideLetterText": null,
    "PlanAssetFund": null,
    "AuditYearEnd": null,
    "AuditYearEndString": ""
},
{
    "Id": 237146,
    "Name": "P Sylebra Ltd.",
    "VehicleTypeId": 7,
    "VehicleTypeName": "EnTrustPermal SPF - Standard",
    "InvestmentManager": null,
    "NavReportingCycleId": null,
    "NavReportingCycleName": "",
    "CurrencyId": null,
    "CurrencyName": "",
    "SideLetterAgreement": false,
    "SideLetterText": null,
    "PlanAssetFund": null,
    "AuditYearEnd": null,
    "AuditYearEndString": ""
},
{
    "Id": 237147,
    "Name": "Sylebra Capital Partners (Onshore), Ltd",
    "VehicleTypeId": 9,
    "VehicleTypeName": "",
    "InvestmentManager": null,
    "NavReportingCycleId": null,
    "NavReportingCycleName": "",
    "CurrencyId": null,
    "CurrencyName": "",
    "SideLetterAgreement": false,
    "SideLetterText": null,
    "PlanAssetFund": null,
    "AuditYearEnd": null,
    "AuditYearEndString": ""
  }
]

1 个答案:

答案 0 :(得分:0)

filter返回一个数组,尝试获取它的第一个元素。 this.FundTerms.filter(x => x.Id === value.Id)[0];