The character "f" is considered true in ng-show
When using angular, I had a very strange problem:
<div ng-app>
<textarea ng-model="resp" placeholder="Response"></textarea>
<a ng-show="resp">Respond</a>
</div>
When writing something in the text box, a link to the answer is displayed.
However, oddly enough, when writing the letter 'f', the answer button is not displayed.
The workaround for this is to use the condition resp.length>0, but I wonder why the letter 'f' behaves in a special way.
+4
4 answers
In fact, "f" counts false.
The AngularJS function uses an internal function toBooleanto evaluate ng-show/ expressions ng-hide.
"f", "false", "0", "n", "no" "[]".
: https://github.com/angular/angular.js/issues/1229.
angular toBoolean:
function toBoolean(value) {
if (typeof value === 'function') {
value = true;
} else if (value && value.length !== 0) {
var v = lowercase("" + value);
value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
} else {
value = false;
}
return value;
}
:
AngularJS 1.3+ .
: - bdfc9c02 'f', '0', 'false', 'no', 'n', '[]' . JavaScript, , : false, null, undefined, NaN, 0 "".
+8