Knockout's writable computed observable in AngularJS (including Ng -modle) m1
Knockout's writable computed observable in AngularJS (including Ng -modle) m1
by manoj
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="fullName"/>
<ul>
<li>{{firstName}}</li>
<li>{{lastName}}</li>
<li>{{fullName}}</li>
</ul>
</div>
</div>
JavaScript
function Ctrl($scope){
$scope.firstName = 'mano';
$scope.lastName = 'manoj';
Object.defineProperty($scope, '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] || '';
}
});
//$scope.fullName = 'Manoj kumar';
}