could you provide me an example how to do it with Angular2?
a.) model -> view
TS:
myString: string = "ABCDEFGH";
Template:
{{myString | lowercase}}
Output:
abcdefgh
2>
Using transformation directly
Template:
Below Input field will have lowercase string as value
<input type="text" [value]="myString.toLowerCase()">
I'm also lowercase: {{myString.toLowerCase()}}
Output:
Input field with value "abcdefgh"
I'm also lowercase: abcdefgh
3>
Using Getter/Setter
TS:
myString: string = "ABCDEFGH";
get stillMyString() {
return this.myString.toLowerCase();
}
set stillMyString(v) {
this.myString = v;
}
Template:
{{stillMyString}}
Output:
abcdefgh
OR using a combination of any of the above
b.) view -> model
Template:
Below Input field will get lowercase string as value but will store uppercase string
<input type="text" [value]="myString.toLowerCase()" (change)="myString = $event.toUpperCase()">
I'm give uppercase values automatically: {{myString}}
Output:
Input field with initial value "abcdefgh"
I'm given uppercase values automatically: ABCDEFGH
2>
Using Setter/Getter
TS:
myString: string = "ABCDEFGH";
get stillMyString() {
return this.myString;
}
set stillMyString(s) {
this.myString = s.toUpperCase();
}
Template:
Below Input field will get lowercase string as value but will store uppercase string
<input type="text" [value]="stillMyString.toLowerCase()" (change)="myString = $event">
Now I'm Uppercase: {{stillMyString}}
Output:
Input field with initial value "abcdefgh"
I'm given uppercase values automatically: ABCDEFGH
AND/OR a combination of above methods and any other method that I can't think of right now.
Wrapping UP
As you can see there are multiple ways to do the same thing, it just depends upon your need and choice to use any of it.
Validation is not the concern of transformation, but you can do it by improving upon the getter/setters
and using FormControl
on your input fields
I could just show you the tip of the iceberg
here, there is so much to model <> view
transformations, becasuse that's what Angular
does, Data Binding
, one way or two way
.
Hope It Helps :)