I am having some issues about replacing. I am fetching some integers from DB. And if the number fetched is just 1 then I replace it with "empty", but when I do that, it affects all numbers that have 1 in them.
Here is what I am doing, first calling toString and then replace
<tr v-if="moreIndex < one.length" v-for="moreIndex in morePro">
<td>{{one[moreIndex].products}}</td>
<td>{{priceSep(one[moreIndex].price).toString().replace("1", "empty")}}</td>
</tr>
So I am changing "1" to empty but if any other data has includes 1 then output look like this...
5empty98
How can I fix this problem?
You can check the length with Conditional (ternary) operator
.:
<td>{{priceSep(one[moreIndex].price).toString().length == 1 ? priceSep(one[moreIndex].price).toString().replace("1", "empty") : priceSep(one[moreIndex].price).toString()}}</td>
May be, you can try directly set the value if the value is 1:
<td>{{priceSep(one[moreIndex].price).toString() == "1" ? "empty" : priceSep(one[moreIndex].price).toString()}}</td>
You can check the length of the string before replacing it
{{priceSep(one[moreIndex].price).toString().length > ?
priceSep(one[moreIndex].price).toString() :
priceSep(one[moreIndex].price).toString().replace("1", "empty")
}}
You can use RegExp:
{{priceSep(one[moreIndex].price).toString().replace(/^1$/, "empty")}}
Related
Is it possible to return empty string when some value is "0", but from HTML page using angularJs?
If I have something like this:
<p>External | Year of Birth: {{profileCtrl.person.year}}</p>
Can I write some expression right there that will check the value for profileCtrl.person.year? If the value is "0" then return empty string, else return value of profileCtrl.person.year.
Something like this is very easy to do in languages like C#, but since I am very new to Angular I was unable to find out if this technology has a power to do such thing?
Can someone help me out with this maybe?
You can do it like this:
{{profileCtrl.person.year === 0 ? "" : profileCtrl.person.year}}
You can use a ternary operator to achieve this;
{{profileCtrl.person.year === 0 ? "" : profileCtrl.person.year}}
This basically says, if it's 0 provide "", else provide the year.
Hope it helps!
You could use the ternary operator ?:
{{ profileCtrl.person.year === 0 ? "" : profileCtrl.person.year }}
You could use a logical OR ||, which uses the second value if the first value is falsy.
<p>External | Year of Birth: {{profileCtrl.person.year || ""}}</p>
Another solution could be to show the wole part, with ng-show attribute and a truthy value. On a falsy value the part is hidden.
<p ng-show="profileCtrl.person.year">External | Year of Birth: {{profileCtrl.person.year}}</p>
Use ternary operator:
<p>External | Year of Birth: {{profileCtrl.person.year !== 0 ? profileCtrl.person.year : ""}}</p>
Another approach might be (haven't checked):
<p>External | Year of Birth: {{profileCtrl.person.year || ""}}</p>
you can use filter in your code for return empty
//js file
app.filter("filterName",function(){
return function(input){
switch(input){
case 0:
return "";
}
});
<!-- html page -->
{{input | filterName}}
I have the following :
<div>{{animalType}}</div>
which evaluates to dog.
Can I conditionally add an sif animalTypeevaluates to anything other than dog?
Something like this : which doesn't work
<div>{{animalType}} {{'s' : !animalType == 'dog'}}</div>
So I would getcats
Use ternary operator in expression as follow:
<div>{{animalType}}{{animalType != 'dog' ? 's' : '' }}</div>
Alternatively
<div>{{animalType}}<span ng-show="animalType!='dog'">s</span></div>
Not tested, but try this simple snippet:
<div>{{animalType === "dog" ? animalType : "s"}}</div>
By the way, if you want to switch only between two values, from my point of view, logically appealing would be something like that:
<div>{{isDog ? "dog" : "s"}}</div>
$scope.isDog = true/false
You could also use ng-if
<div>{{animalType}}<div ng-if="animalType !== 'dog'">s</div></div>
I have an object property that may or may not contain a number and that number may or may not be equal to 0. For the moment, I have this:
var TheVar = parseInt(SomeObject.SomeVar, 10);
if (!TheVar > 0) {
TheVar = "-";
}
I want TheVar to be either a positive number or "-". I'm just wondering if my conditional statement is going to cover every case?
Thanks for your suggestions.
No. You are missing parentheses.
if( !(TheVar > 0))
NaN > 0 returns false, so the if condition will go through.
I want a minimum string value to use in my comparator. Assume that my validation prevents name from being the empty string.
This appears to be working correctly. Are there any values of "name" for which it will fail?
S.FileList = Backbone.Collection.extend
model: S.File
comparator: (file) ->
# We add display files alphabetically, but with meta.file at the top.
if file.get("name") == "meta.file"
return ""
return file.get("name")
Assuming your validation prevents name from being the empty string, and enforces it being a string: Yes, this will work. "" < str where str is any string other than "".
Again, you've got to make sure that typeof name is 'string', because while
"" < "0"
is true,
"" < 0
is false.
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 9 years ago.
I want to check if string b is completely contained in string a.
I tried:
var a = "helloworld";
var b = "wold";
if(a.indexOf(b)) {
document.write('yes');
} else {
document.write('no');
}
The output is yes, it is not my expected output, because string b(wold) is not completely contained in string a(helloworld) --- wold v.s. world
Any suggestion to check the string?
Read the documentation: MDC String.indexOf :)
indexOf returns the index the match was found. This may be 0 (which means "found at the beginning of string") and 0 is a falsy value.
indexOf will return -1 if the needle was not found (and -1 is a truthy value). Thus the logic on the test needs to be adjusted to work using these return codes. String found (at beginning or elsewhere): index >= 0 or index > -1 or index != -1; String not found: index < 0 or index == -1.
Happy coding.
You need to use if(a.indexOf(b) > -1) instead. indexOf returns -1 when it can't find a string.
.indexOf returns -1 if no match was found, which is a truthy value. You'll need to check more explicitly:
if (a.indexOf(b) != -1)
That's because indexOf returns -1 if a value is not found:
if(a.indexOf(b) != -1) {
you may want to use this
if(a.indexOf(b) != -1)
You need to test if the result is -1. -1 indicates no match, but evaluates to true in a boolean sense.
var a = "helloworld";
var b = "wold";
if(a.indexOf(b) > -1) {
document.write('yes');
} else {
document.write('no');
}