One of the interesting things AngularJS can do is apply a filter to a particular databinding expression, which is a convenient way to apply, for example, culture-specific currency or date formatting of a model's properties. It is also nice to have computed properties on the scope. The problem is that neither of these features work with two-way databinding scenarios - only one-way databinding from the scope to the view. This seems to be a glaring omission in an otherwise excellent library - or am I missing something?
In KnockoutJS, I could create a read/write computed property, which allowed me to specify a pair of functions, one which is called to get the value of the property, and one which is called when the property is set. This allowed me to implement, for example, culture-aware input - letting the user type "$1.24" and parsing that into a float in the ViewModel, and have changes in the ViewModel reflected in the input.
The closest thing I could find similar to this is the use of $scope.$watch(propertyName, functionOrNGExpression);
This allows me to have a function invoked when a property in the $scope
changes. But this doesn't solve, for example, the culture-aware input problem. Notice the problems when I try to modify the $watched
property within the $watch
method itself:
$scope.$watch("property", function (newValue, oldValue) {
$scope.outputMessage = "oldValue: " + oldValue + " newValue: " + newValue;
$scope.property = Globalize.parseFloat(newValue);
});
(http://jsfiddle.net/gyZH8/2/)
The input element gets very confused when the user starts typing. I improved it by splitting the property into two properties, one for the unparsed value and one for the parsed value:
$scope.visibleProperty= 0.0;
$scope.hiddenProperty = 0.0;
$scope.$watch("visibleProperty", function (newValue, oldValue) {
$scope.outputMessage = "oldValue: " + oldValue + " newValue: " + newValue;
$scope.hiddenProperty = Globalize.parseFloat(newValue);
});
(http://jsfiddle.net/XkPNv/1/)
This was an improvement over the first version, but is a bit more verbose, and notice that there is still an issue of the parsedValue
property of the scope changes (type something in the second input, which changes the parsedValue
directly. notice the top input does not update). This might happen from a controller action or from loading data from a data service.
Is there some easier way to implement this scenario using AngularJS? Am I missing some functionality in the documentation?