Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS string replace on template

Why this return a blank string on my template?

{{ selectedVehicle.air_conditioning.replace(true, "Yes") }}
like image 933
Fabrizio A Avatar asked Feb 16 '15 11:02

Fabrizio A


People also ask

How to replace a character in a string in Angular js?

The replace() method can be used to search the particular character(s), which is getting replaced with a specific character(s) & return a new generated string. Syntax: string. replace(char findChar, char replacedChar);

How do you replace a string?

replace() Parameters The replace() method can take maximum of 3 parameters: old - old substring you want to replace. new - new substring which will replace the old substring. count (optional) - the number of times you want to replace the old substring with the new substring.

How replace all occurrences of a string in TypeScript?

To replace all occurrences of a string in TypeScript, use the replace() method, passing it a regular expression with the g (global search) flag. For example, str. replace(/old/g, 'new') returns a new string where all occurrences of old are replaced with new .

What is template strings?

Template strings are a powerful feature of modern JavaScript released in ES6. It lets us insert/interpolate variables and expressions into strings without needing to concatenate like in older versions of JavaScript. It allows us to create strings that are complex and contain dynamic elements.


2 Answers

.replace method can't be applied on Boolean variable, it works on string only. That's why you need to convert it to string then apply replace.

{{ selectedVehicle.air_conditioning.toString().replace(true, "Yes") }}

Working Fiddle

Hope this could help you. Thanks.

like image 131
Pankaj Parkar Avatar answered Sep 22 '22 04:09

Pankaj Parkar


Okay, if I get it:

  • air_conditioning is a boolean ?
  • you want to print yes when air_conditioning is true ?

you can just use ternary syntax like this:

{{ selectedVehicle.air_conditioning ? "Yes" : "No }}
like image 28
tanou Avatar answered Sep 21 '22 04:09

tanou