Tôi có một thành phần trong góc 4 được gọi là ba lần. Trong siêu dữ liệu mẫu tôi có một div với một chỉ thị với một số ràng buộc như thế này.Nhiều trường hợp của chỉ thị góc 4 được gọi là từ một thành phần mesed lên các giá trị đầu vào
@import {gServ} from '../gServ.service';
@Component: ({
selector: 'sr-comp',
template: `<div gDirective [cOptions]="dataChart">`
})
export class SGComponent implements OnInit {
@Input('report') public report: IReportInstance;
cOptions:any;
constructor(private gServ: gServ) {
}
ngOnInit(){
this.cOptions = {};
this.cOptions = this.gServ.objectMerge(this.gServ.defaultOpt, this.report.opt);
//this.report.opt is binded to a component when is instantiated.
//this.gServ.objectMerge is a function that merge the two objects
}
}
this.cOptions thay đổi cho mỗi trường hợp của các thành phần, sau đó trong chỉ thị tôi có điều này:
import { Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';
@Directive({
selector: '[gDirective]'
})
export class SGDirective implements OnInit {
public _element: any;
@Input() public cOptions: string;
constructor(public element: ElementRef) {
this._element = this.element.nativeElement;
}
ngOnInit() {
console.log(this.cOptions);
}
}
Vấn đề là console.log(this.cOptions);
luôn in cùng một đối tượng, ngay cả khi thành phần thiết cOptions
với các giá trị khác nhau trong phương thức ngOnInit
.
Bạn có ý tưởng gì không?
Tôi có vấn đề về simillar. – Marek
Bạn đã thử 'console.log' trực tiếp kết quả của dịch vụ, trong thành phần của bạn' ngOnInit'. 'this.cOptions = this.gServ.objectMerge (...); console.log (this.cOptions); ' –