I have an issue with AngularJS (version 1.2.6).
For some reason that I could not understand I cannot access the length
property of a string variable stored in the $scope
.
In the template:
String '{{myObject.someVariable}}' has length '{{myObject.someVariable.length}}'.
In the controller:
$scope.myObject = {} ;
//asynchronuous loading of myObject
SomeService.loadObject(function(result)){
$scope.myObject = result ;
console.log("Content: '%s', length:'%i'",$scope.myObject.someVariable,$scope.myObject.someVariable.length);
$scope.$apply();
});
The result is :
String 'aaaa' has length ''.
In the console I correctly see Content:'aaaa', length:'4'
This is ennoying because I display (or not) some parts of the template depending on the string size.
Update
$scope.myObject.someVariable
is a string. I added a breakpoint in the callback function with two watches :
$scope.myObject.someVariable
: "aaaa"typeof($scope.myObject.someVariable)
: "string"Is someVariable
a string? If not, you might have to cast it to a string before accessing the length property. Any easy way is to concatenate it with an empty string:
{{(myObject.someVariable + '').length}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With