Making click event in TS in Angular 2+












1















Normally when adding a click event in Angular you would write this



<div (click)="myfunc()">click me </div>


but I want to do something like this in TS instead



`<div (click)="${this.myfunc()}">click me</div>`


It works a bit, but the click is made on load.
Then I tried this



`<div (click)="() => ${this.myfunc}">click me</div>`


But that did not work at all. Any help ?



Here is how I call the modal, where I want to use the code



openModalWithComponent(img) {
const initialState = {
title: 'Modal with component',
content: `
<img src="${img}" />
<div (click)="${this.onImageRemoveHandler()}">delete</div>`,
header: false,
footer: false
};

this.bsModalRef = this._modalService.show(
ModalContentComponent, {initialState});
}









share|improve this question

























  • Why would you do that in the first place ? Seems like an XY problem

    – trichetriche
    Nov 19 '18 at 13:03











  • because I am calling a modal component, where I want to inject some code

    – Johansrk
    Nov 19 '18 at 14:03











  • And how is changing the syntax going to change how you call your modal ? We would need the framework used for the modal and the code of the function to help you. Otherwise, only the first solution would work.

    – trichetriche
    Nov 19 '18 at 14:05











  • I updated my description, so you can see the modal

    – Johansrk
    Nov 19 '18 at 14:12






  • 1





    I mean you have to explain why you would use this syntax, this is a template litteral syntax and it's used for strings. So again, explain the issue, not what you think is the solution.

    – trichetriche
    Nov 19 '18 at 14:47
















1















Normally when adding a click event in Angular you would write this



<div (click)="myfunc()">click me </div>


but I want to do something like this in TS instead



`<div (click)="${this.myfunc()}">click me</div>`


It works a bit, but the click is made on load.
Then I tried this



`<div (click)="() => ${this.myfunc}">click me</div>`


But that did not work at all. Any help ?



Here is how I call the modal, where I want to use the code



openModalWithComponent(img) {
const initialState = {
title: 'Modal with component',
content: `
<img src="${img}" />
<div (click)="${this.onImageRemoveHandler()}">delete</div>`,
header: false,
footer: false
};

this.bsModalRef = this._modalService.show(
ModalContentComponent, {initialState});
}









share|improve this question

























  • Why would you do that in the first place ? Seems like an XY problem

    – trichetriche
    Nov 19 '18 at 13:03











  • because I am calling a modal component, where I want to inject some code

    – Johansrk
    Nov 19 '18 at 14:03











  • And how is changing the syntax going to change how you call your modal ? We would need the framework used for the modal and the code of the function to help you. Otherwise, only the first solution would work.

    – trichetriche
    Nov 19 '18 at 14:05











  • I updated my description, so you can see the modal

    – Johansrk
    Nov 19 '18 at 14:12






  • 1





    I mean you have to explain why you would use this syntax, this is a template litteral syntax and it's used for strings. So again, explain the issue, not what you think is the solution.

    – trichetriche
    Nov 19 '18 at 14:47














1












1








1








Normally when adding a click event in Angular you would write this



<div (click)="myfunc()">click me </div>


but I want to do something like this in TS instead



`<div (click)="${this.myfunc()}">click me</div>`


It works a bit, but the click is made on load.
Then I tried this



`<div (click)="() => ${this.myfunc}">click me</div>`


But that did not work at all. Any help ?



Here is how I call the modal, where I want to use the code



openModalWithComponent(img) {
const initialState = {
title: 'Modal with component',
content: `
<img src="${img}" />
<div (click)="${this.onImageRemoveHandler()}">delete</div>`,
header: false,
footer: false
};

this.bsModalRef = this._modalService.show(
ModalContentComponent, {initialState});
}









share|improve this question
















Normally when adding a click event in Angular you would write this



<div (click)="myfunc()">click me </div>


but I want to do something like this in TS instead



`<div (click)="${this.myfunc()}">click me</div>`


It works a bit, but the click is made on load.
Then I tried this



`<div (click)="() => ${this.myfunc}">click me</div>`


But that did not work at all. Any help ?



Here is how I call the modal, where I want to use the code



openModalWithComponent(img) {
const initialState = {
title: 'Modal with component',
content: `
<img src="${img}" />
<div (click)="${this.onImageRemoveHandler()}">delete</div>`,
header: false,
footer: false
};

this.bsModalRef = this._modalService.show(
ModalContentComponent, {initialState});
}






angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:24









JJJ

29.1k147591




29.1k147591










asked Nov 19 '18 at 12:55









JohansrkJohansrk

3,59122425




3,59122425













  • Why would you do that in the first place ? Seems like an XY problem

    – trichetriche
    Nov 19 '18 at 13:03











  • because I am calling a modal component, where I want to inject some code

    – Johansrk
    Nov 19 '18 at 14:03











  • And how is changing the syntax going to change how you call your modal ? We would need the framework used for the modal and the code of the function to help you. Otherwise, only the first solution would work.

    – trichetriche
    Nov 19 '18 at 14:05











  • I updated my description, so you can see the modal

    – Johansrk
    Nov 19 '18 at 14:12






  • 1





    I mean you have to explain why you would use this syntax, this is a template litteral syntax and it's used for strings. So again, explain the issue, not what you think is the solution.

    – trichetriche
    Nov 19 '18 at 14:47



















  • Why would you do that in the first place ? Seems like an XY problem

    – trichetriche
    Nov 19 '18 at 13:03











  • because I am calling a modal component, where I want to inject some code

    – Johansrk
    Nov 19 '18 at 14:03











  • And how is changing the syntax going to change how you call your modal ? We would need the framework used for the modal and the code of the function to help you. Otherwise, only the first solution would work.

    – trichetriche
    Nov 19 '18 at 14:05











  • I updated my description, so you can see the modal

    – Johansrk
    Nov 19 '18 at 14:12






  • 1





    I mean you have to explain why you would use this syntax, this is a template litteral syntax and it's used for strings. So again, explain the issue, not what you think is the solution.

    – trichetriche
    Nov 19 '18 at 14:47

















Why would you do that in the first place ? Seems like an XY problem

– trichetriche
Nov 19 '18 at 13:03





Why would you do that in the first place ? Seems like an XY problem

– trichetriche
Nov 19 '18 at 13:03













because I am calling a modal component, where I want to inject some code

– Johansrk
Nov 19 '18 at 14:03





because I am calling a modal component, where I want to inject some code

– Johansrk
Nov 19 '18 at 14:03













And how is changing the syntax going to change how you call your modal ? We would need the framework used for the modal and the code of the function to help you. Otherwise, only the first solution would work.

– trichetriche
Nov 19 '18 at 14:05





And how is changing the syntax going to change how you call your modal ? We would need the framework used for the modal and the code of the function to help you. Otherwise, only the first solution would work.

– trichetriche
Nov 19 '18 at 14:05













I updated my description, so you can see the modal

– Johansrk
Nov 19 '18 at 14:12





I updated my description, so you can see the modal

– Johansrk
Nov 19 '18 at 14:12




1




1





I mean you have to explain why you would use this syntax, this is a template litteral syntax and it's used for strings. So again, explain the issue, not what you think is the solution.

– trichetriche
Nov 19 '18 at 14:47





I mean you have to explain why you would use this syntax, this is a template litteral syntax and it's used for strings. So again, explain the issue, not what you think is the solution.

– trichetriche
Nov 19 '18 at 14:47












0






active

oldest

votes











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%2f53375118%2fmaking-click-event-in-ts-in-angular-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53375118%2fmaking-click-event-in-ts-in-angular-2%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







這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong