Macabeus Macabeus - 1 year ago 49
AngularJS Question

Why ng-bind does not work with array?

I am wanting to create a bind to an element with an array, for when the array is changed, the HTML to be updated. But, it's not work.


<body ng-controller="Game as game">

<span ng-bind="game.testString"></span>

<span ng-bind="game.testArray"></span>

<input type="button" ng-click="game.btnAddLetter()" value="Change">


angular.module('notesApp', [])
.controller('Game', [function() {
var self = this;
self.testString = 'a';
self.testArray = ['a'];

self.btnAddLetter = function() {
self.testString = 'X';

I expected the two span would be updated, however, only the
has been updated. For what reason did this happen? How to solve this problem?

Answer Source

You must use the JSON filter:

<span ng-bind="game.testArray | json"></span>