0

I'm VERY new to Ionic/Angular/Typescript...

I have the following button:

<button ion-button (click)="shareMe('hello')">Hello</button>
<button ion-button (click)="shareMe('world')">World</button>

And I have the following function:

shareMe(){
this.sharingVar.share($STRING)
.then(()=>{
    alert("Success");
  },
  ()=>{
     alert("failed");
  })
};

Basically, when I click a given button, I want to pass a VARIABLE to the function to process. The code above obviously doesn't work... but how do you do this in Angular/Typescript?

Is there a way to pass a variable $string to a function?

Please be as descriptive as you can. Much thanks.

1 Answer 1

3

You just need to add the parameter to the shareMe() function.

shareMe(shareOption: string){
    this.sharingVar.share(shareOption)
        .then(() => {
            alert("Success");
        })
        .catch(() => {
            alert("failed");
        });
};

Update: added @Aluan Haddad suggestion because you are learning and this kind of things is something that you need to know now, not after you wrote 5k LoC.

Sign up to request clarification or add additional context in comments.

6 Comments

Indeed this is quite correct. Consider improving your use of promises by either (1) using catch(() => {}) instead of the second argument to thenwhich has awkward composability or (2) using async/await with try/catch.
@AluanHaddad I'm not familiar with either concept you've introduced. Can you give me a bit more info, or point me in the right direction for me to learn? Thanks much.
@camaron, I get how you setup the function. How would I change the code in the button? This didn't work: <button ion-button (click)="shareMe('world')">World</button>
Why certainly. I will create a gist in a little bit.
@camaron UPDATE: Actually it worked like beautifully. I had a little bug. Thank you so much!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.