你好,我是 Angular 5 的新手,我在 appmodule 中提供了共享服务,这就是服务
import { DetailTransaksi } from '../models/DetailTransaksiModel'
@Injectable()
export class TransaksiService {
UserTrans:Transaksi;
constructor() {
this.UserTrans = new Transaksi();
}
getTransaksi(){
return this.UserTrans;
}
addDetail(data:DetailTransaksi){
this.UserTrans.ListDetail.push(data);
}
countDetail(){
return this.UserTrans.ListDetail.length;
}
}
子组件在里面router-outlet
,然后在子组件中我调用addDetail()
了方法,但是当我调用时父组件app.component没有更新countDetail
,这是我的父组件
import { Component, OnInit } from '@angular/core';
import { TransaksiService } from './services/transaksi.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
numPesanan:number;
constructor(private transService:TransaksiService){
}
ngOnInit(){
this.numPesanan = this.transService.countDetail();
}
}