تعامل و انتقال اطلاعات بین کامپوننتها در Angular – بخش دوم
نویسنده: جواد رسولی
تاریخ: ۱۳۹۶/۰۹/۰۵ ۱۴:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
@Input('readOnly') FormIsReadOnly: boolean; <app-customer-info FormIsReadOnly="true"></app-customer-info>
@Component({
selector: 'app-customer-info',
templateUrl: './customer-info.component.html',
styleUrls: ['./customer-info.component.css']
})
export class CustomerInfoComponent implements OnInit {
private _formIsReadOnly: boolean;
@Input()
set FormIsReadOnly(value: boolean) {
if (typeof (value) != 'boolean')
throw new Error(`${value} type is not boolean.`);
this._formIsReadOnly = value;
}
get FormIsReadOnly(): boolean { return this._formIsReadOnly; }
constructor() { }
ngOnInit() {
}
} import { Component, OnInit, Input, OnChanges, SimpleChange } from '@angular/core';
import { ICustomerInfo } from '../../core/model/ICustomerInfo';
@Component({
selector: 'app-customer-info',
template: '<ul>< li *ngFor="let change of changeLog">{{change }}</li></ul>',
styleUrls: ['./customer-info.component.css']
})
export class CustomerInfoComponent implements OnChanges {
changeLog: string[] = [];
@Input() FormIsReadOnly: boolean;
ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
let log: string[] = [];
for (let propName in changes) {
let changedProp = changes[propName];
let to = JSON.stringify(changedProp.currentValue);
if (changedProp.isFirstChange()) {
log.push(`Initial value of ${propName} set to ${to}`);
} else {
let from = JSON.stringify(changedProp.previousValue);
log.push(`${propName} changed from ${from} to ${to}`);
}
}
this.changeLog.push(log.join(', '));
}
constructor() { }
} • Initial value of FormIsReadOnly set to true • FormIsReadOnly changed from true to "trued" • FormIsReadOnly changed from "trued" to "true" • FormIsReadOnly changed from "true" to "truef" • FormIsReadOnly changed from "truef" to "true" • FormIsReadOnly changed from "true" to "tru" • FormIsReadOnly changed from "tru" to "tr" • FormIsReadOnly changed from "tr" to "t" • FormIsReadOnly changed from "t" to "" • FormIsReadOnly changed from "" to "t" • FormIsReadOnly changed from "t" to "tr" • FormIsReadOnly changed from "tr" to "tru" • FormIsReadOnly changed from "tru" to "true"
در ادامه عنوان «بهجریان انداختن رخدادها از کامپوننت فرزند و گرفتن آنها را از طریق کامپوننت پدر» را مورد برسی قرار خواهیم داد.
ادامه دارد/