javascript - angular controller variable changes when set afterwards? -
var jem = 55; var app = angular.module("store",[]); app.controller("storecontroller",function(){ this.product = jem; }); jem = 0;
<!doctype html> <html ng-app="store"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script> <meta charset="utf-8"> <title>js bin</title> </head> <body ng-controller="storecontroller store"> <p>{{"hi"}}</p> <p>{{store.product}} </p> </body> </html>
why output "0" instead of "55"? since jem basic javascript variable when product assigned jem gets value copied , should not change when jem changed?
notice controller definition inside callback function (as should be...)
app.controller("storecontroller", function(){ this.product = jem; });
a side effect of this, relevant question, assignment statement within callback, this.product = jem
, executed after assignment statement, jem = 0
, outside of callback.
the takeaway callbacks not take place sequentially rest of code.
Comments
Post a Comment