Sunday 15 June 2014

angularjs - level 2 of angular.js tutorial -



angularjs - level 2 of angular.js tutorial -

i saw in angular.js tutorial on level 2:

(function() { var app = angular.module('gemstore', []); app.controller('storecontroller', function(){ this.products = gems; }); app.controller("tabcontroller", function(){ this.tab = 1; this.settab = function(selectedtab){ this.tab = selectedtab; }; }); var gems = [// lot of code here...] })();

my uncertainty is: how settab function alter value of tabcontroler's tab variable? if utilize 'this.tab' in settab function, settab function assigning variable it's own internal scope, doesn't it?

if utilize 'this.tab' in settab function, settab function assigning variable it's own internal scope, doesn't it?

actually, no.

this, within settab function reference particular instance of tabcontroller. so, this.tab, means "the tab property of tabcontroller instance". instance created angular internals handle view, don't worry part.

edit:

you can utilize example:

var myconstructor = function() { this.tab = 0; this.settab = function(newtab) { this.tab = newtab; }; this.logtab = function() { console.log(this.tab); }; }; var myobj = new myconstructor(); myobj.logtab(); // prints 0 myobj.settab(2); myobj.logtab(); // prints 2

but, dave mentioned, stick $scope approach.

angularjs

No comments:

Post a Comment