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 8655233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:58:41+00:00 2026-06-12T14:58:41+00:00

Short Question: How to create a <input type=text> which contains a custom-format string serialization

  • 0

Short Question:

How to create a <input type="text"> which contains a custom-format string serialization of an object in a way that editing the string updates the model and vice versa?

I think AngularJS’ directives are the way to go, but i can’t get it pinned down.

Long Question:

Prequel

I have a object which is my application’s “master model”. it can be serialized to a string of a specific format:

  1. it has 2-3 attributes, whose serializations are joined by “;” (no trailing “;” if the third is missing)

  2. attributes 2 and 3 are lists of objects, and serialized by joining those with “,”.

  3. the serialization of the objects is just one of their string attributes, or two ow them with “x” between.

so i have a constructor (accepting a spec string), and a toString function. Following; the latter for clarity:

World.prototype.toString = function() {
    var spec = [];

    spec[0] = this.version;
    spec[1] = this.layers.map(function(layer) {
        var c = (layer.c > 1) ? layer.c + 'x' : '';
        return c + layer.id; //e.g. 'T' or '2xT'
    }).join(',');

    //spec[2] in python: ','.join(option.id for option in options if option.checked)

    var options = this.options.filter(function(option) {
        return option.checked;
    });
    if (options.length > 0)
        spec[2] = options.map(function(option) {
            return option.id;
        }).join(',');

    return spec.join(';');
};

The directive i tried to use looks thusly, but the $watch only fires once.

angular.module('layersApp', []).directive('spec', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            scope.$watch('world', function(val) {
                element.val(val.toString());
                console.log('object updated', element.val());
            }, true);
            element.blur(function(e) {
                scope.world = new World(element.val());
            });
        }
    };
});

Actual long question

What i want is an easy way to make this work,

<input type="text" data-ng-model="theWorld" spec>

where spec is the custom directive shown above, setting up the two-way binding

Outlook

it would be awesome if this could result in a generic “serialization” directive used like that:

<input type="text" data-serialization="string2Foo, foo2String" data-ng-model="foo">

Which would look up the object foo, and the functions string2Foo and foo2String to setup custom (de)serialization.

  • 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-12T14:58:42+00:00Added an answer on June 12, 2026 at 2:58 pm

    I think you can use of $parsers and $filters of ngModel controller.
    Here is the simplest example of doing it.
    http://plnkr.co/edit/13PJN2

    It should be easy to add validation, too.

    I tried to make it accept custom serializer from parent scope, but failed to do so. Not sure about it.

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

Sidebar

Related Questions

Short Question: Can I specify wildcards for custom console folding? If so, what is
Short question: Is the parent of an entity group included in that entity group
Short question: I'm trying to make that in a given page (uses tabs) back
In my ASP.NET MVC 2 web application, I allow users to create custom input
Here is the code that will help you understand my question: create table con
Short question, is the following ok: struct X { A& x; A y; X()
Short question: Is it possible to detect window.open() in a UIWebView using the UIWebViewDelegate
Short Question: What is the importance of isomorphic functions in programming (namely in functional
Short question: Do any of MS's built in Data Objects support INotifyPropertyChanged? Long explination:
Short question - how do you define your view models? Here are some of

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.