Call ngAfterViewInit of Angular component from common place












0















I am using bootstrap tooltip.
I want to trigger ngAfterViewInit for every component from some common place.



Common place(like app.module.ts)



ngAfterViewInit() {
$('[data-toggle="tooltip"]').tooltip();
}


Component_1:
There is no ngAfterViewInit in component, still life cycle is trigger from some common place.



Component_2:
This component has ngAfterViewInit,



ngAfterViewInit() {
// Some extra functionality
}


In this case ngAfterViewInit is triggered from both the places(common place as well as from it's respective component)



Is there any way to achieve this?



Thanks










share|improve this question























  • AfterViewInit is an interface, just extends from a component class and use wherever you want to use

    – Prashant Pimpale
    Jan 1 at 7:06











  • A lifecycle hook that is called after Angular has fully initialized a component's view. Define a ngAfterViewInit() method to handle any additional initialization tasks. so each component has there owned responsibility

    – Prashant Pimpale
    Jan 1 at 7:07


















0















I am using bootstrap tooltip.
I want to trigger ngAfterViewInit for every component from some common place.



Common place(like app.module.ts)



ngAfterViewInit() {
$('[data-toggle="tooltip"]').tooltip();
}


Component_1:
There is no ngAfterViewInit in component, still life cycle is trigger from some common place.



Component_2:
This component has ngAfterViewInit,



ngAfterViewInit() {
// Some extra functionality
}


In this case ngAfterViewInit is triggered from both the places(common place as well as from it's respective component)



Is there any way to achieve this?



Thanks










share|improve this question























  • AfterViewInit is an interface, just extends from a component class and use wherever you want to use

    – Prashant Pimpale
    Jan 1 at 7:06











  • A lifecycle hook that is called after Angular has fully initialized a component's view. Define a ngAfterViewInit() method to handle any additional initialization tasks. so each component has there owned responsibility

    – Prashant Pimpale
    Jan 1 at 7:07
















0












0








0








I am using bootstrap tooltip.
I want to trigger ngAfterViewInit for every component from some common place.



Common place(like app.module.ts)



ngAfterViewInit() {
$('[data-toggle="tooltip"]').tooltip();
}


Component_1:
There is no ngAfterViewInit in component, still life cycle is trigger from some common place.



Component_2:
This component has ngAfterViewInit,



ngAfterViewInit() {
// Some extra functionality
}


In this case ngAfterViewInit is triggered from both the places(common place as well as from it's respective component)



Is there any way to achieve this?



Thanks










share|improve this question














I am using bootstrap tooltip.
I want to trigger ngAfterViewInit for every component from some common place.



Common place(like app.module.ts)



ngAfterViewInit() {
$('[data-toggle="tooltip"]').tooltip();
}


Component_1:
There is no ngAfterViewInit in component, still life cycle is trigger from some common place.



Component_2:
This component has ngAfterViewInit,



ngAfterViewInit() {
// Some extra functionality
}


In this case ngAfterViewInit is triggered from both the places(common place as well as from it's respective component)



Is there any way to achieve this?



Thanks







javascript angular twitter-bootstrap angular-lifecycle-hooks






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 7:00









AnkurAnkur

422319




422319













  • AfterViewInit is an interface, just extends from a component class and use wherever you want to use

    – Prashant Pimpale
    Jan 1 at 7:06











  • A lifecycle hook that is called after Angular has fully initialized a component's view. Define a ngAfterViewInit() method to handle any additional initialization tasks. so each component has there owned responsibility

    – Prashant Pimpale
    Jan 1 at 7:07





















  • AfterViewInit is an interface, just extends from a component class and use wherever you want to use

    – Prashant Pimpale
    Jan 1 at 7:06











  • A lifecycle hook that is called after Angular has fully initialized a component's view. Define a ngAfterViewInit() method to handle any additional initialization tasks. so each component has there owned responsibility

    – Prashant Pimpale
    Jan 1 at 7:07



















AfterViewInit is an interface, just extends from a component class and use wherever you want to use

– Prashant Pimpale
Jan 1 at 7:06





AfterViewInit is an interface, just extends from a component class and use wherever you want to use

– Prashant Pimpale
Jan 1 at 7:06













A lifecycle hook that is called after Angular has fully initialized a component's view. Define a ngAfterViewInit() method to handle any additional initialization tasks. so each component has there owned responsibility

– Prashant Pimpale
Jan 1 at 7:07







A lifecycle hook that is called after Angular has fully initialized a component's view. Define a ngAfterViewInit() method to handle any additional initialization tasks. so each component has there owned responsibility

– Prashant Pimpale
Jan 1 at 7:07














1 Answer
1






active

oldest

votes


















0














There isn't "one common place" where you can define a global behavior for all of your components.



Instead, you can create the following class and make your desired components extend it:



export abstract class BaseComponent implements AfterViewInit {
/**
* View initialization behavior. Note that derived classes must not implement AfterViewInit .
* Use viewInit() on derived classes instead.
*/
ngAfterViewInit(): void {
// Your code
this.viewInit();
}

/**
* Function that allows derived components to define an additional view initialization behavior
*/
abstract viewInit(): void;
}





share|improve this answer


























  • I think you misunderstood the question, I want some common place from where I can all the ngAfterViewInit. Not only from component. I want extra hook or listener.

    – Ankur
    Jan 5 at 4:17











  • @Ankur please take a look at my new answer

    – YoukouleleY
    Jan 7 at 17:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993613%2fcall-ngafterviewinit-of-angular-component-from-common-place%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














There isn't "one common place" where you can define a global behavior for all of your components.



Instead, you can create the following class and make your desired components extend it:



export abstract class BaseComponent implements AfterViewInit {
/**
* View initialization behavior. Note that derived classes must not implement AfterViewInit .
* Use viewInit() on derived classes instead.
*/
ngAfterViewInit(): void {
// Your code
this.viewInit();
}

/**
* Function that allows derived components to define an additional view initialization behavior
*/
abstract viewInit(): void;
}





share|improve this answer


























  • I think you misunderstood the question, I want some common place from where I can all the ngAfterViewInit. Not only from component. I want extra hook or listener.

    – Ankur
    Jan 5 at 4:17











  • @Ankur please take a look at my new answer

    – YoukouleleY
    Jan 7 at 17:41
















0














There isn't "one common place" where you can define a global behavior for all of your components.



Instead, you can create the following class and make your desired components extend it:



export abstract class BaseComponent implements AfterViewInit {
/**
* View initialization behavior. Note that derived classes must not implement AfterViewInit .
* Use viewInit() on derived classes instead.
*/
ngAfterViewInit(): void {
// Your code
this.viewInit();
}

/**
* Function that allows derived components to define an additional view initialization behavior
*/
abstract viewInit(): void;
}





share|improve this answer


























  • I think you misunderstood the question, I want some common place from where I can all the ngAfterViewInit. Not only from component. I want extra hook or listener.

    – Ankur
    Jan 5 at 4:17











  • @Ankur please take a look at my new answer

    – YoukouleleY
    Jan 7 at 17:41














0












0








0







There isn't "one common place" where you can define a global behavior for all of your components.



Instead, you can create the following class and make your desired components extend it:



export abstract class BaseComponent implements AfterViewInit {
/**
* View initialization behavior. Note that derived classes must not implement AfterViewInit .
* Use viewInit() on derived classes instead.
*/
ngAfterViewInit(): void {
// Your code
this.viewInit();
}

/**
* Function that allows derived components to define an additional view initialization behavior
*/
abstract viewInit(): void;
}





share|improve this answer















There isn't "one common place" where you can define a global behavior for all of your components.



Instead, you can create the following class and make your desired components extend it:



export abstract class BaseComponent implements AfterViewInit {
/**
* View initialization behavior. Note that derived classes must not implement AfterViewInit .
* Use viewInit() on derived classes instead.
*/
ngAfterViewInit(): void {
// Your code
this.viewInit();
}

/**
* Function that allows derived components to define an additional view initialization behavior
*/
abstract viewInit(): void;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 7 at 17:40

























answered Jan 1 at 21:11









YoukouleleYYoukouleleY

2,6961826




2,6961826













  • I think you misunderstood the question, I want some common place from where I can all the ngAfterViewInit. Not only from component. I want extra hook or listener.

    – Ankur
    Jan 5 at 4:17











  • @Ankur please take a look at my new answer

    – YoukouleleY
    Jan 7 at 17:41



















  • I think you misunderstood the question, I want some common place from where I can all the ngAfterViewInit. Not only from component. I want extra hook or listener.

    – Ankur
    Jan 5 at 4:17











  • @Ankur please take a look at my new answer

    – YoukouleleY
    Jan 7 at 17:41

















I think you misunderstood the question, I want some common place from where I can all the ngAfterViewInit. Not only from component. I want extra hook or listener.

– Ankur
Jan 5 at 4:17





I think you misunderstood the question, I want some common place from where I can all the ngAfterViewInit. Not only from component. I want extra hook or listener.

– Ankur
Jan 5 at 4:17













@Ankur please take a look at my new answer

– YoukouleleY
Jan 7 at 17:41





@Ankur please take a look at my new answer

– YoukouleleY
Jan 7 at 17:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993613%2fcall-ngafterviewinit-of-angular-component-from-common-place%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith