I am totally new to AngularJs but I have managed to learn the basics of the framework. However, I have recently encountered an exotic behavior from AngularJs ,which is known for being dynamic.
Here is the issue:
Index.html
<div ng-controller="userClick as clk">
<div id="square1">{{ clk.board[0] }}</div>
</div>
app.js
app.controller('userClick', function($timeout) {
this.board = ['', '', '', '', '', '', '', '', ''];
$timeout(reset, 1000);
function reset (){
this.board = ['', '', '', '', '', '', '', '', ''];
console.log(this.board); //returns ^^ (empty array)
}
It is expected that when the user clicks on one of the squares on the page, that a function is executed and everything goes well. But when I try to use the $timeout function to delay the response for a second, the array this.board gets reset successfully to an empty one but the div there with the id square1 doesn't gets empty back again.
It seems that something causes Angular to be static at this point that I don't get.
Note: It is a Tic Tac Toe game.
thisinside reset() function isn't samethisas outside function. Has nothing to do with angular , is a javascript scope issuethisin variable and use that variable inside function instead ofthis. See github.com/johnpapa/angular-styleguide/blob/master/a1/…