Comments (7)
@phongca22 Việc dùng setter vs ngOnChanges
là gần như như nhau dành cho single Input
. Tuy nhiên, ở trường hợp cần check nhiều Inputs
cùng lúc thì ngOnChanges
là sự lựa chọn tốt hơn.
from 100-days-of-angular.
@phongca22 Theo anh một trong những lợi thế của ngOnChanges là em có thể check được giá trị hiện tại, và giá trị trước đó, hoặc lần thay đổi đó có phải là lần đầu tiên hay ko.
Ví dụ như em có component với @Input text: string
như sau
export class TestComponent implements OnChanges {
@Input() text: string;
constructor() { }
ngOnChanges(changes: SimpleChanges) {
let textChange = changes.text;
}
}
Thì biến textChange sẽ là kiểu SimpleChange với khá nhiều property hữu dụng. Mỗi khi value của text thay đổi thì em sẽ access dc đến các property này ở trong hàm ngOnChanges
export declare class SimpleChange {
previousValue: any;
currentValue: any;
firstChange: boolean;
constructor(previousValue: any, currentValue: any, firstChange: boolean);
/**
* Check whether the new value is the first value assigned.
*/
isFirstChange(): boolean;
}
from 100-days-of-angular.
@phongca22 mình reopen để làm như documentation cho repo. Label answered
có ý nghĩa như câu hỏi đã đc trả lời. Thank you nhé :D
from 100-days-of-angular.
Trong trường hợp khởi tạo component dynamically thì mình phải tự call lifecycle do đó lúc này có thể dùng setter thay cho Input, nó sẽ tự động gọi hàm set thì chúng ta có thể làm thêm side effect ở đây.
from 100-days-of-angular.
@phhien203 2 cách đều gọi hàm thì đâu có khác gì nhau???
from 100-days-of-angular.
Theo như @nartc thì với single input mình sẽ dùng setter/getter nhanh và gọn hơn. Trong khi dùng ngOnChanges phải implement OnChanges rồi override lại ngOnChanges() nữa.
Nếu nhiều input thì phải dùng ngOnChanges rồi.
Thanks @tieppt @trungk18 @nartc
from 100-days-of-angular.
Mình xin bổ sung thêm 1 ý nữa đó là nếu mình muốn viết code theo style imperative thì mình dùng ngOnChanges, còn nếu muốn viết theo style declarative thì dùng getters.
from 100-days-of-angular.
Related Issues (20)
- Tuyệt vời cảm ơn các anh
- Translation? HOT 2
- Chưa thấy lifecycle anh nhỉ? HOT 1
- 100 ngày với angular
- Day 42. Giới thiệu state management, what, why, how
- Day 43. ngrx action, reducer, selector
- Day 44. ngrx effect
- Day 38 - Gặp lỗi khi lazy load component có dùng *ngIf hoặc cần import từ module khác HOT 6
- Day 37 - Async Validator HOT 3
- Day 38 - Template Form Custom Validator
- Validate FormGroup với 1 vài control HOT 4
- Day 37: Async Validator trong Angular Form - về cách tạo custom validators HOT 2
- Angular HOT 1
- Angular 100 HOT 1
- 100days angular
- Thank you HOT 1
- Angular
- Spanish Traslation HOT 2
- Angular
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 100-days-of-angular.