Ok what I am trying to do is count the number of characters inside an element, but the element includes binding (eg. {{data.username}}) and I want to get the string length after the binding occurs.
My thinking so far has been to create a attribute directive and just .text().length the element that is passed into the "link" function — see below:
This was my working so far:
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>Counting Characters</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js"></script>
</head>
<body ng-controller="TestCtrl">
<div count-chars>{{person.firstName}} {{person.lastName}}</div>
<script>
var app = angular.module('app', []);
app.controller('TestCtrl', function ($scope) {
$scope.person = {
firstName: "James",
lastName: "Smith"
};
});
app.directive('countChars', [function(){
return {
link: function(scope, elem, attrs) {
console.log(elem.text());
console.log(elem.text().length);
}
};
}]);
</script>
</body>
</html>
The problem is that this only returns the string before any bindings occurs (via the console.logss at the moment). What I would want to get is James Smith and 11 characters, but what I get is {{person.firstName}} {{person.lastName}} and 40 chars.
Any ideas?