analog for Knockout's writable computed observable in AngularJS
using nested property analog for Knockout's writable computed observable in AngularJS? http://stackoverflow.com/questions/19526938/what-is-the-analog-for-knockouts-writable-computed-observable-in-angularjs?lq=1
by jarnal
HTML
<div ng-app>
<div ng-controller="Ctrl">
<input type="text" ng-model="firstName"/>
<input type="text" ng-model="lastName"/>
<input type="text" ng-model="test.child.grandchild.fullName"/>
<ul>
<li>{{firstName}}</li>
<li>{{lastName}}</li>
<li>{{test.child.grandchild.fullName}}</li>
</ul>
</div>
</div>
JavaScript
function Ctrl($scope){
$scope.firstName = 'John';
$scope.lastName = 'Doe';
$scope.test.child.grandchild = {};
var d = $scope.test.child.grandchild;
Object.defineProperty(d, 'fullName', {
get: function(){
return $scope.lastName ? $scope.firstName+' '+$scope.lastName : $scope.firstName;
},
set: function(newValue){
var values = newValue.split(' ');
$scope.firstName = values[0] || '';
$scope.lastName = values[1] || '';
}
});
}