ALAIN TORRES
ALAIN TORRES

Reputation: 67

How can I make a condition in a button in angular?

my idea is that if today's date is greater than or equal to the expected date that the button directs to "x" or "y"...

I don't know where can I put this condition (if on file .ts or .html):

var expectedDate = new Date ("2022/08/11")
var dates = expectedDate.toString()
var today = new Date().toISOString().split('T')[0];
if(today >= expectedDate) {
 microsoftLogin()
}
else{...
googleLogin()
}

Here is my code in .HTML where is my button Login I don't know if is possible to add the condition on (tap)

<Button 
width="70%" 
height="40" 
(tap)="CONDITION HERE"
text="Iniciar sesión" 
class="login-submit" 
row="2">
</Button>

Upvotes: 0

Views: 1829

Answers (2)

natDaGreat
natDaGreat

Reputation: 76

I don't know much about native script but you should be able to make a method in your TS file and call it in the HTML

loginTapped() {
 var expectedDate = new Date ("2022/08/11")
 var dates = expectedDate.toString()
 var today = new Date().toISOString().split('T')[0];
 if(today >= expectedDate) {
  microsoftLogin()
 }
 else{...
 googleLogin()
 }
}
<Button 
width="70%" 
height="40" 
(tap)="loginTapped()"
text="Iniciar sesión" 
class="login-submit" 
row="2">
</Button>

Hopefully this helps.

Upvotes: 1

Mehran Khan
Mehran Khan

Reputation: 1165

This may help you.

expectedDate=null;
today=null;

onLogin() {
  this.expectedDate = new Date('2022/01/11');
  this.today =new Date();

  if(this.today.getTime() > this.expectedDate.getTime())
  {
    // today is greater than expected
    this.microsoftLogin()
  }
  else
  {
    // today is less than expected
    this.googleLogin()
  }
}

microsoftLogin()
{
  // some function logic
}

googleLogin()
{
  // some function logic
}
<button 
  width="70%" 
  height="40" 
  (click)="onLogin()"
  class="login-submit" 
  row="2">
  Login
</button>

Upvotes: 0

Related Questions