javascript - Getters and Setters on angular.extend $scope -
i'm reading interesting article angularjs: tricks angular.extend(), , there explained how use getters , setters extend. there points don't understand.
js:
app.controller('thingcontroller', [ '$scope', function($scope) { // private var _thingone = 'one', _thingtwo = 'two'; // models angular.extend($scope, { thingone() { console.log("get thing1"); return _thingone + 'eeeeee'; }, set thingone(value) { console.log("set thing 1"); if (value !== 'one' && value !== 'two'){ throw new error('invalid value ('+value+') thingone'); } }, thingtwo() { console.log("get thing2"); return _thingtwo + 'oooooo'; }, set thingtwo(value) { console.log("set thing 2"); if (value !== 'two' && value !== 'three'){ throw new error('invalid value ('+value+') thingtwo'); } } }); // methods angular.extend($scope, { things() { return _thingone + ' ' + _thingtwo; } }); }]); html:
<body ng-controller="thingcontroller"> {{ things }} // one, 2 </body> my questions:
¿why on
get thingone()not returnedoneeeeeee? (the sameget thingtwo())¿where called
set thingone,set thingtwo? because never see on consoleset thing 1orset thing 2.¿how can declare
set methodinsideangular.extend methods? ¿it possible assing value view?
try way:
angular.extend($scope, { things() { return $scope.thingone + ' ' + $scope.thingtwo; } }); setter called in case:
$scope.thingone = 'test';
Comments
Post a Comment