HKI345
HKI345

Reputation: 313

Using ng-if for displaying text based on file extension type inside ng-repeat (new to angular)

Here i have scenario where i have to display text 'Y' if file extension is 'pdf','xlsx' and 'docx' else 'N'

<tr ng-repeat = "file in filedetails">
<td>
    <span ng-if ="(file .filename.substring(file.filename.lastIndexOf('.') + 1) === ('pdf'))">Y</span>
    <span ng-if ="(file.filename.substring(file.filename.lastIndexOf('.') + 1) === ('xlsx'))">Y</span>
    <span ng-if ="(file.filename.substring(file.filename.lastIndexOf('.') + 1) === ('docx'))">Y</span>
    <span ng-if ="(file.filename.substring(file.filename.lastIndexOf('.') + 1) !== (('xlsx') || ('pdf') || ('docx')))">N</span></td></tr>

with the above code im able to display 'N' for all the unsupported file extensions but along with for 'pdf' and 'docx'. only 'xlsx' is working fine for 'N' with this condition(Y is working fine for all)..How can i achieve this?

I have one more concern with this question....if i want to display N if it is not of one of the extension 'xlsx','pdf','docx' along with one more condtion if it is of one of the extension 'xlsx','pdf','docx' and validating file.conversion_status === ('SKIPPED') too and display Y with one of the extension being true.please help on this

Upvotes: 0

Views: 1221

Answers (2)

kshetline
kshetline

Reputation: 13682

Unfortunately this:

(('xlsx') || ('pdf') || ('docx')))

...simply evaluates to 'xlsx', and all you're doing is checking that the extension doesn't equal 'xlsx'. Shortening the expression for the extension down to 'ext', what you need to test is this:

ext !== 'xlsx' && ext !== 'pdf' && ext !== 'docx'

Upvotes: 1

S&#233;bastien
S&#233;bastien

Reputation: 12139

This line will not work as expected:

(file.filename.substring(file.filename.lastIndexOf('.') + 1) !== (('xlsx') || ('pdf') || ('docx')))

You need 2 operands for every OR (||). This would be:

(file.filename.substring(file.filename.lastIndexOf('.') + 1) !== 'xlsx') ||
(file.filename.substring(file.filename.lastIndexOf('.') + 1) !== 'pdf') ||
(file.filename.substring(file.filename.lastIndexOf('.') + 1) !== 'docx')

Upvotes: 0

Related Questions