Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

angular - Operator comparison in an *ngIf

I want to compare number in an ngIf and depending on the result i'll display something.

  <div>
      <div *ngIf="number(user.number)>59">
     {{  number(user.number) }}
      </div>
      <div *ngIf="number(user.number)<60">
        {{  number(user.number) }}
        </div>
    </div>

my number.ts

 number ( date: Date) {
    const now = new Date().getFullYear()
    const diff = Math.abs(now - new Date(date).getFullYear())
    console.log(diff)
    return diff 
}

it will return a number

This is not working i think there's a problem with ">" & "<" operator, is there a way to make it work ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the interior of *ngIf (value between "") must end up in being evaluable to truthy/false value. The issue might be in your number() implementation, usages of > & < are perfectly fine.

Please provide your function implementation and wider context i.e. from where user.number comes from

--EDIT--
After OP posted TS code, there will be issue with date format. The part: new Date(date).getFullYear() in case of incorrect value will evaluate to 1970, then result will be always 48, which is less than both values compared. You need to validate your input in date creation.

Try in browser console to create any of following:

new Date(99)
new Date(2000)
new Date(20)

they will all evaluate to Thu Jan 01 1970 01:00:00 GMT+0100. Supplement Date constructor with dummy parameter:

new Date(99, 0)
new Date(2000, 0)
new Date(20, 0)

to get valid dates. But still, this is just an assumption, since you are not providing us format of user.number, i just assumed this is input text value in range 0 to infinity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...