KnockoutJS Observables Observables are special JavaScript objects that are used widely in KnockoutJS to handle the data exchange between view-model and view. Here in this tutorial we are going to explain how observables works in KnockoutJS.
KnockoutJS Observables | Syntax | Example
KnockoutJS basically works on the following three features-
- 1.Observables and dependency tracking – Observables plays an important in data exchange between View and View-Model. Observables takes care of dependency tracking.
- 2.Declarative bindings– data-bind is used in view to handle the data(update) in ViewModel.
- 3.Templating– KnockoutJs supports templating which enables us to create rich applications.
Syntax
KnockoutJS Observables Syntax:
this.propertyName = ko.observable('value'); |
You can just make any property observable. ko is globally available once you include the KnockouJS library. ‘value’ will be assigned to the view model property ie. to propertyName.
Example
Now let us create very basic example to understand the observables-
KnockoutJS Observables Example:
<title>KnockoutJS Example</title> <div> <p>Hello Welcome to <span data-bind="text: helloText"><span></span></span></p> </div> |