-
Notifications
You must be signed in to change notification settings - Fork 27.5k
ngIf and ngShow mistreats "[]" (empty array) #3969
Comments
I am not sure I agree with this. From my chrome console:
|
OK, I think I see what you are saying. So it seems like a design choice. See https://github.com/angular/angular.js/blob/master/src/Angular.js#L838 |
@petebacondarwin. From the following two examples the first will log if ('[]') {
console.log('a');
} else {
console.log('b');
} if ([]) {
console.log('a');
} else {
console.log('b');
} |
@petebacondarwin: you're right, and it's like that for more than 3 years now in https://github.com/angular/angular.js/blob/a7d62dcb5533ceb9a7ae47ee27e2054400a0196b/src/Angular.js But if it's by design should it return nothing? Shouldn't it trigger some kind of error informing developer to pass custom function implementing logic and returning either true or false? |
@petebacondarwin It seems illogical that Our use case is that we download a JSON from the server with some fields populated with user data. One of these fields should trigger showing a particular element only if the user actually wrote anything in that field (i.e. it's a non-empty string). But one of the things user can write is |
the javascript truthiness type coercion is crazy too though. (all of these are falsy: |
@IgorMinar That's not true, everything you mentioned is truthy... As is every object, even ones without any keys. JS has some crazy coercion rules so to be clear, the value if (x) {
console.log('a');
} else {
console.log('b');
} |
Make ng-if evaluate things using javascript logic Do not reevaluate everything contained within an ng-if when there is a change of the value of the ng-if, but the truthy of falsy value does not change Fixes angular#3969 BREAKING CHANGE: The expressions * `<div ng-if="[]">X</div>` * `<div ng-if="'f'">X</div>` * `<div ng-if="'[]'">X</div>` used to be evaluated to `false`
Is this resolved? All the boolean angular expressions I've seen are using angular's toBoolean method, which treats empty arrays as falsy. I agree with @mzgol, it doesn't make any sense to allow JavaScript boolean logic inside an expression and then evaluate the result using something completely different. "[]" -> false, "![]" -> false (what!?), "!![]" -> true (makes sense, but then you'd expect "[]" to be truthy too) |
Make ngShow and ngHide follow javascript `truthy`/`falsy` logic and not the custom toBoolean logic Fixes angular#5414 angular#4277 angular#3969 BREAKING CHANGE: The expressions * `<div ng-hide="[]">X</div>` * `<div ng-hide="'f'">X</div>` * `<div ng-hide="'[]'">X</div>` used to be evaluated to `false` and the elements were hidden. The same effect is present for `ng-show` and the elements are now visible; and with `ng-if` and the elements are now removed If you were previously doing `ng-show="exp"` where `$scope.exp = 'no' // (or 'n' or 'f')`, then instead write `ng-show="exp && exp !== 'no'` (or 'n' or 'f').
Make ngIf, ngShow and ngHide follow javascript `truthy`/`falsy` logic and not the custom toBoolean logic Fixes angular#5414 angular#4277 angular#3969 BREAKING CHANGE: The expressions * `<div ng-hide="[]">X</div>` * `<div ng-hide="'f'">X</div>` * `<div ng-hide="'[]'">X</div>` used to be evaluated to `false` and the elements were hidden. The same effect is present for `ng-show` and the elements are now visible; and with `ng-if` and the elements are now removed If you were previously doing `ng-show="exp"` where `$scope.exp = 'no' // (or 'n' or 'f')`, then instead write `ng-show="exp && exp !== 'no'` (or 'n' or 'f').
Make ngIf, ngShow and ngHide follow javascript `truthy`/`falsy` logic and not the custom toBoolean logic Fixes angular#5414 angular#4277 angular#3969 BREAKING CHANGE: The expressions * `<div ng-hide="[]">X</div>` * `<div ng-hide="'f'">X</div>` * `<div ng-hide="'[]'">X</div>` used to be evaluated to `false` and the elements were hidden. The same effect is present for `ng-show` and the elements are now visible; and with `ng-if` and the elements are now removed If you were previously doing `ng-show="exp"` where `$scope.exp = 'no' // (or 'n' or 'f')`, then instead write `ng-show="exp && exp !== 'no'` (or 'n' or 'f').
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy; only JavaScript falsy values are treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, o and "". Fixes angular#3969 Fixes angular#4277
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy. Only JavaScript falsy values are now treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, o and "". Fixes angular#3969 Fixes angular#4277
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy. Only JavaScript falsy values are now treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, o and "". Fixes angular#3969 Fixes angular#4277
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy. Only JavaScript falsy values are now treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, 0 and "". Fixes angular#3969 Fixes angular#4277
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application Thanks to lgalfaso for test ideas. BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy. Only JavaScript falsy values are now treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, 0 and "". Fixes angular#3969 Fixes angular#4277
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application Thanks to lgalfaso for test ideas. BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy. Only JavaScript falsy values are now treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, 0 and "". Fixes angular#3969 Fixes angular#4277
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application Thanks to lgalfaso for test ideas. BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy. Only JavaScript falsy values are now treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, 0 and "". Fixes angular#3969 Fixes angular#4277
So far Angular have used the toBoolean function to decide if the parsed value is truthy. The function made more values falsy than regular JavaScript would, e.g. strings 'f' and 'no' were both treated as falsy. This creates suble bugs when backend sends a non-empty string with one of these values and something suddenly hides in the application Thanks to lgalfaso for test ideas. BREAKING CHANGE: values 'f', '0', 'false', 'no', 'n', '[]' are no longer treated as falsy. Only JavaScript falsy values are now treated as falsy by the expression parser; there are six of them: false, null, undefined, NaN, 0 and "". Closes angular#3969 Closes angular#4277 Closes angular#7960
If ngIf/ngShow if given an argument of a string of 2 characters: "[" and "]" it is treated as if it would be [] - empty array, which is neither true nor false.
Here's a test (utilizing ngIf only)
http://jsfiddle.net/VeJfM/1/
The text was updated successfully, but these errors were encountered: