Code Monkey home page Code Monkey logo

Comments (7)

nartc avatar nartc commented on April 29, 2024 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.

trungvose avatar trungvose commented on April 29, 2024 3

@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.

nartc avatar nartc commented on April 29, 2024 2

@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.

tieppt avatar tieppt commented on April 29, 2024 1

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.

tieppt avatar tieppt commented on April 29, 2024 1

@phhien203 2 cách đều gọi hàm thì đâu có khác gì nhau???

from 100-days-of-angular.

phongca22 avatar phongca22 commented on April 29, 2024

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.

phhien203 avatar phhien203 commented on April 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.