[英]Angular variable value change doesn't reflect in DOM
我有一个 Angular 组件( SummaryComponent
),它注入了一个服务( LicenseRequestService
)。 组件的变量 ( totalLicense
) 被定义为等于来自服务的计算字段。
组件类编写如下:
import { Component, OnInit } from '@angular/core';
import { LicenseRequestService } from '../shared/license-request.service';
@Component({
selector: 'sg-summary',
templateUrl: './summary.component.html',
styleUrls: ['./summary.component.css']
})
export class SummaryComponent implements OnInit {
constructor(private _service : LicenseRequestService) {
}
public totalLicense= this._service.licenseDetails.length
ngOnInit(): void {
}
updateSummary(){
const licenses= this._service.licenseDetails.length
console.log("total players requested : " + players)
this.totalLicense= players
}
}
我已将totalLicense
值嵌入到 DOM 中,如下所示:
<div>
Total number of licenses is : {{totaLicense}}
</div>
<button type="submit" (click)="updateSummary()">Get licenses</button>
每次服务更新时,我都希望 DOM 反映更新后的totalLicense
值,但它保持为 0。但是,当我单击标记为Get licenses
的按钮时,DOM 随即更新,并且totaLicense
变量在浏览器中显示新值。
我是否需要进行任何更改以确保在不单击按钮的情况下更新totalLicense
的值(这是所需的功能)?
尝试把这个public totalLicense= this._service.licenseDetails.length
代码constructor
或ngOnInit
您的问题是您没有传递引用,而是传递了一个值。 基本上,如果你有以下几点:
let x = 'test'; let y = x; x = 'update'; console.log(y);
我可以临时想到三种解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.