0

I have a app built with Angular and Angular UI. I am trying to count the characters across multiple accordions, as displayed in the plunker. The code I have only returns the first two characters and then nothing?

Is there something wrong I am doing?

The plunk is: Example code

app.js

$scope.what=[];
$scope.why=[];

Object.defineProperty($scope, 'characters', {
    get() {
        return $scope.what.length + $scope.why.length;
    }
});

HTML:

{{characters}} 

But like I said this only returns the first two characters?

Sole
  • 3,100
  • 14
  • 58
  • 112
  • 2
    Because ```what``` and ```why``` are arrays. You're storing text at ```what[$index]``` but reading ```what``` –  Aug 26 '15 at 09:22

2 Answers2

1

why and what are arrays, if you want to count all the characters in these arrays, try :

Object.defineProperty($scope, 'characters', {
        get() {
            return $scope.what.join('').length + $scope.why.join('').length;
        }
});
Thierry
  • 5,133
  • 3
  • 25
  • 30
  • Hi Thierry, The code above causes problems in IE and Safari - in safari i get SyntaxError: Unexpected token '(' (anonymous function)controllers.js:38 angular.js:4373 – Sole Sep 01 '15 at 11:21
  • @Sole what is the exact code in line 38 of controllers.js? I suppose there is a js function not supported in Safari? – Thierry Sep 01 '15 at 15:11
  • its is: Object.defineProperty($scope, 'characters', { – Sole Sep 01 '15 at 16:25
  • Object.defineProperty doesn't work in IE8. May be your Safari version is very old? See http://stackoverflow.com/questions/17271224/object-defineproperty-polyfill for workaround – Thierry Sep 01 '15 at 18:17
0

You could write up a function with a for loop to count the index of what and why and then place that in the AngularJS.

TheLimeTrees
  • 413
  • 6
  • 20