מה ההבדל באנגולר בין constructor ל ngOnInit ?

KATZ

משתמש פעיל
Constructor
אחראי ליצירת המופעים, ולהזרקת שירותים הנדרשים לקומפוננטה (injector).
ngOnInit
בסך הכל פונקציה המופעלת בעת טעינת הקומפוננטה.

בתוך ה- constructor לא נשים לוגיקה או פעולות, רק מופעים שצריך לאתחל, או שירותים שצריך להצהיר עליהם ולעשות להם - inject
 

ST_B

משתמש מקצוען
הקונסטרקטור הוא הבנאי של הקומפוננטה,
הוא משמש בד"כ להגדרת משתנים שישמשו לכל אורך החיים של הקומפוננטה.
כמו סרוויסים וכדו'
הוא מאותחל ישר עם בניית הקומפוננטה והעלייה שלה לאוויר.
OnInit קורה אחרי הקונסטרקטור, לפני איתחול הקומפוננטה שכולל את התצוגה והכל.
משמש לאיתחול מערכים ומשתנים פנימיים.
בעצם קונסטרקטור קורה עוד לפני תחלית מחזור החיים של הקומפוננטה, ו OnInit זה תחילת מחזור החיים.
מקווה שהובנתי...;)
בהצלחה!!
 

yitzhak123

משתמש סופר מקצוען
הנדסת תוכנה
In Angular, `constructor` and `ngOnInit` are two lifecycle hooks that serve different purposes. Here's a brief explanation of each:

1. **constructor: The constructor method in an Angular component is a special method that is automatically called when a new instance of the class is created. It is typically used for the dependency injection of services and the initialization of class fields. The constructor method is a default method of the TypeScript class. You should not use it to call HTTP services or set values that depend from parameters of the component or values that depend from dynamic parameters.

2. **ngOnInit: This is a lifecycle hook specific to Angular, called once the component is initialized. This is the best place to put any initialization logic for the component. It runs after the constructor method and is generally used for component initialization tasks.

Here's the main differences between them:

- The `constructor` is a default method of the class that is executed when the class is instantiated and ensures proper initialization of fields in the class and its subclasses. Angular or any other libraries cannot ensure when the class instantiation happens.

- `ngOnInit` is a life cycle hook called by Angular2+ to indicate that Angular is done creating the component. We have to import `OnInit` in order to use it (from `@angular/core`). Most of the time we use `ngOnInit` for all the initialization/declaration and avoid stuff to work in the constructor.

- It's good practice to avoid HTTP calls or any business logic in the `constructor`. Instead, use the `constructor` for dependency injection and simple initializations like field initializations, and use `ngOnInit` for initialization logic.

- It's important to note that at the time the constructor execution takes place, the Angular bindings aren't initialized yet. If you try to access inputs, they would be undefined. But in `ngOnInit`, the bindings are already initialized, so you can access inputs, the DOM, and perform complex initializations.​
 

מתכנתת -

משתמש מקצוען
איור וציור מקצועי
באנגולר לפי מה שידוע לי לא מקובל לשים בconstractor פעולות וכו' מעבר ליצירת מופעים של אוביקטים חיצוניים.
פעולות שרוצים שיקרו בטעינת הדף זה ב- oninit
כל שאר הפעולות והקוד בפונקציות רגילות.
בהצלחה!
 

בינה מלכותית

משתמש מקצוען
תכלס אם אני שם הכל רק בקונסטרקטור זה משנה?
זה יכול לפגוע משהו?
כן יש הבדל קטן(אולי בגרסאות המתקדמות כבר פחות)
ברעיון בקונסטרקטור המערכת עוד לא העלתה את כל משתני המחלקה,
ngOnINit המערכת כבר סיימה עם כל ההצהרות ועכשיו מעלה את הHTML
 

אולי מעניין אותך גם...

הפרק היומי

הפרק היומי! כל ערב פרק תהילים חדש. הצטרפו אלינו לקריאת תהילים משותפת!


תהילים פרק קלז

א עַל נַהֲרוֹת בָּבֶל שָׁם יָשַׁבְנוּ גַּם בָּכִינוּ בְּזָכְרֵנוּ אֶת צִיּוֹן:ב עַל עֲרָבִים בְּתוֹכָהּ תָּלִינוּ כִּנֹּרוֹתֵינוּ:ג כִּי שָׁם שְׁאֵלוּנוּ שׁוֹבֵינוּ דִּבְרֵי שִׁיר וְתוֹלָלֵינוּ שִׂמְחָה שִׁירוּ לָנוּ מִשִּׁיר צִיּוֹן:ד אֵיךְ נָשִׁיר אֶת שִׁיר יְהוָה עַל אַדְמַת נֵכָר:ה אִם אֶשְׁכָּחֵךְ יְרוּשָׁלִָם תִּשְׁכַּח יְמִינִי:ו תִּדְבַּק לְשׁוֹנִי לְחִכִּי אִם לֹא אֶזְכְּרֵכִי אִם לֹא אַעֲלֶה אֶת יְרוּשָׁלִַם עַל רֹאשׁ שִׂמְחָתִי:ז זְכֹר יְהוָה לִבְנֵי אֱדוֹם אֵת יוֹם יְרוּשָׁלִָם הָאֹמְרִים עָרוּ עָרוּ עַד הַיְסוֹד בָּהּ:ח בַּת בָּבֶל הַשְּׁדוּדָה אַשְׁרֵי שֶׁיְשַׁלֶּם לָךְ אֶת גְּמוּלֵךְ שֶׁגָּמַלְתְּ לָנוּ:ט אַשְׁרֵי שֶׁיֹּאחֵז וְנִפֵּץ אֶת עֹלָלַיִךְ אֶל הַסָּלַע:
נקרא  11  פעמים

לוח מודעות

למעלה