Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:14:28+00:00 2026-06-13T00:14:28+00:00

One of the interesting things AngularJS can do is apply a filter to a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:14:29+00:00Added an answer on June 13, 2026 at 12:14 am

    It turns out that there’s a very elegant solution to this, but it’s not well documented.

    Formatting model values for display can be handled by the | operator and an angular formatter. It turns out that the ngModel that has not only a list of formatters but also a list of parsers.

    1. Use ng-model to create the two-way data binding

    <input type="text" ng-model="foo.bar"></input>
    

    2. Create a directive in your angular module that will be applied to the same element and that depends on the ngModel controller

    module.directive('lowercase', function() {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function(scope, element, attr, ngModel) {
                ...
            }
        };
    });
    

    3. Within the link method, add your custom converters to the ngModel controller

    function fromUser(text) {
        return (text || '').toUpperCase();
    }
    
    function toUser(text) {
        return (text || '').toLowerCase();
    }
    ngModel.$parsers.push(fromUser);
    ngModel.$formatters.push(toUser);
    

    4. Add your new directive to the same element that already has the ngModel

    <input type="text" lowercase ng-model="foo.bar"></input>
    

    Here’s a working example that transforms text to lowercase in the input and back to uppercase in the model

    The API Documentation for the Model Controller also has a brief explanation and an overview of the other available methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is one of the more interesting things I've seen in iPhone development. The
I found interesting things.. Following code doesn't show @One and it show @Two after
Interesting one here. I have an ASP.NET 1.1 project that contains a web service
Here is an interesting one... I have an application I am writing for devices
Alright, this one's interesting. I have a solution, but I don't like it. The
Problem One friend suggested an interesting problem. Given the following code: public class OuterClass
One of my team has recently come across an interesting glitch in the matrix.
After updating to php 5.3 one of our systems has developed an interesting bug.
I've got an interesting SQL puzzle I haven't been able to solve, hopefully one
I'm generally not a fan of microbenchmarks. But this one has a very interesting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.