0

I'm actually not sure how to ask this question, and I'm probably using incorrect terms, so bear with me.

Angular sets up a 2-way data binding so that it makes it easy to work with the data on both sides. But what if I want to change how that data is represented?

Let me give a concrete example.

I want a form with a checkbox, which if bound directly to a model, would be stored as true or false by Angular. However, in another part of the webpage I want that value to show up as 0 or 1, not true or false.

Now sure I could go and make them use two different variables, and use ng-change or something like that to update one based on the other, but that seems overkill and convoluted.

Is there some special meta function or something I can define that lets me essentially translate the data as it goes back and forth?

1
  • 1
    it sounds like you're looking for a formatter. Take a look at this answer to see if it gives you what you need. Commented Jun 7, 2014 at 20:29

3 Answers 3

2

Use the ngTrueValue and ngFalseValue directives. They define what should be treated as true and false on a checkbox: https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

Example:

<input type="checkbox" ng-model="foo"
       ng-true-value="OK"
       ng-false-value="BOO-HOO">

The model will either have a value of "OK" or "BOO-HOO" instead of the default true and false in the above example.

Alternatively, if you want the original model to retain its default values and only draw the custom ones from another variable, you could also use the ngChange directive:

<input type="checkbox" ng-model="foo"
       ng-change="bar=foo&&'OK'||'BOO-HOO'">

Now, whenever foo changes, bar will have the corresponding alternative value. Just remember to assign bar an initial value (it will start out with no value at all).

Sign up to request clarification or add additional context in comments.

3 Comments

The OP said "I want that value to show up as 0 or 1", the model should still hold true or false , it's just a change of the rendered view.
Yes, I was just updating the answer to reflect that as you were adding your comment.
Thanks! This does what I needed it to in an elegant way. Brian S's comment about formatters though is also what I was looking for in the larger scope, but the ng-true/false setup does what I need for this specific project.
0

in your controller...

$scope.getvalue(data)
{
if(data===true)
return 1; // write what ever you want...
return 0;
}

in your html page.. bind the normal one as {{$scope.data1}} and other as {{getvalue($scope.data1)}}

Comments

0

You can do some nice things with ngBind:

Check this plunker: http://plnkr.co/edit/cRhLN2p5N4PmI65ps6Gp?p=preview

<input type="checkbox" ng-model="ok"> OK?

<h2>true or false: {{ ok }}</h2>
<h2>0 or 1: {{ ok ? 1 : 0 }}</h2>

3 Comments

Um... ternary expressions don't work in angular expressions. You need to use cond&&val1||val2 instead, else it will break the template :)
@mingos angularjs supports ternary expressions since version 1.1.5, check this feature github.com/angular/angular.js/commit/…
Thank you sir for enlightening me :). I've no idea how I missed this one. We learn something new every day, don't we? Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.