How data binding is working?


#1

Hi all,

I am trying to understand how data binding is working and why there are so many ways.
First of all let’s start with an easy example:
I have a text view:
<TextView class="mainTextArea"></TextView>

I can have one way data binding for the text:
<TextView [text]="mainText" class="mainTextArea"></TextView>

I can have two way data binding for the text:
<TextView [(ngModel)]="mainText" class="mainTextArea"></TextView>

And i can have other bindings like this:
<TextView [(ngModel)]="mainText" class="mainTextArea" enabled="{{enableState}}"></TextView>

Now my problem is that i don’t understand why for example [text]="mainText" works for me, but when i try to use [(ngModel)]="mainText" doesn’t. Isn’t it enought to declare in my component class a class variable “mainText”?


#2

I believe you are using Angular for building apps from your code snippets above. NativeScript just makes it easy for Web / Angular developers to reuse their knowledge and all fancy framework features to build native mobile apps.

[property]=“value” - This is known as property binding (one way).
[(ngModel)]=“value” - This is known as two way data binding.

To know more about how data binding works or what are various options please go through the official Angular docs. If you are not a Angular guy, I would recommend you to use NativeScript with simple JavaScript / TypeScript.


#3

Thank you for your answer.

I believe you are using Angular for building apps from your code snippets above.

Yepp

If you are not a Angular guy, I would recommend you to use NativeScript with simple JavaScript / TypeScript.

Even though i am not an Angular guy :slight_smile: i prefer to use (and learn) it since it brings much more to the code.

To know more about how data binding works or what are various options please go through the official Angular docs.

Yep, this is much better. The information on NativeScript docs is very lacking in my opinion.


#4

I appreciate your interest to learn Angular but honestly it does not make sense expecting Angular to be documented in {N}.

I believe once you learn Angular, then you might find the docs in {N} comforting.