SlideShare a Scribd company logo
1 of 10
PART 4
Bindings
Controlling text and appearance
Knockout’s declarative binding system
provides a concise and powerful way to
link data to the UI.
Binding syntax
Today's message is: <span data-bind="text: myMessage"></span>
A binding consists of two items, the binding name and value,
separated by a colon. Here is an example of a single, simple
binding:
1. The visible binding
2. The text binding
3. The html binding
4. The css binding
5. The style binding
6. The attr binding
 Purpose
• The visible binding causes the associated DOM element to
become hidden or visible according to the value you pass to the
binding.
Example
<div data-bind="visible: shouldShowMessage">
You will see this message only when "shouldShowMessage" holds a
true value.
</div>
<script type="text/javascript">
var viewModel = {
shouldShowMessage: ko.observable(true) // Message initially visible
};
viewModel.shouldShowMessage(false); // ... now it's hidden
viewModel.shouldShowMessage(true); // ... now it's visible again
</script>
 Purpose
• The text binding causes the associated DOM element
to display the text value of your parameter
Example
Today's message is: <span data-bind="text: myMessage"></span>
<script type="text/javascript">
var viewModel = {
myMessage: ko.observable() // Initially blank
};
viewModel.myMessage("Hello, world!"); // Text appears
</script>
 Purpose
• The html binding causes the associated DOM element to
display the HTML specified by your parameter.
Example
<div data-bind=“html: details “></div>
<script type="text/javascript">
var viewModel = {
details : ko.observable()
};
viewModel. details(“<em>Hello, world!<em>”);
</script>
 Purpose
• The css binding adds or removes one or more named CSS
classes to the associated DOM element. This is useful, for
example, to highlight some value in red if it becomes negative.
Example
<div data-bind=“css: { profitWarning: currentProfit() < 0 }“> Profit
Information </div>
<script type="text/javascript">
var viewModel = {
currentProfit : ko.observable(20000)
};
viewModel.currentProfit(-15);
</script>
 Purpose
• The style binding adds or removes one or more style values to
the associated DOM element. This is useful, for example, to
highlight some value in red if it becomes negative, or to set the
width of a bar to match a numerical value that changes.
Example
<div data-bind=“style: { color: currentProfit() < 0 ? 'red' : 'black' }“> Profit
Information </div>
<script type="text/javascript">
var viewModel = {
currentProfit : ko.observable(20000)
};
viewModel.currentProfit(-15);
</script>
 Purpose
• The attr binding provides a generic way to set the
value of any attribute for the associated DOM element.
Example
<a data-bind=“attr: { href: url, title: details}“> Report </a>
<script type="text/javascript">
var viewModel = {
url: ko.observable("year-end.html"),
details: ko.observable("Report including final year-end statistics")
};
</script>
You see, God helps only people who work hard.
That principle is very clear.
- A. P. J. Abdul Kalam

More Related Content

Similar to Knockoutjs Part 4 Bindings Controlling text and appearance

Knockoutjs Part 5 Bindings - Control flow
Knockoutjs Part 5 Bindings - Control flowKnockoutjs Part 5 Bindings - Control flow
Knockoutjs Part 5 Bindings - Control flowBhaumik Patel
 
Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databindingBoulos Dib
 
MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2Naji El Kotob
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library 10Clouds
 
Introduction to Web Technologies PPT.pptx
Introduction to Web Technologies PPT.pptxIntroduction to Web Technologies PPT.pptx
Introduction to Web Technologies PPT.pptxKunal Kalamkar
 
Adding a view
Adding a viewAdding a view
Adding a viewNhan Do
 
How to keep working technically interesting
How to keep working technically interestingHow to keep working technically interesting
How to keep working technically interestingKenichi Murahashi
 
Organize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS TricksOrganize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS TricksAndolasoft Inc
 
Angular js animate shashikant bhongale -20-7-16
Angular js animate shashikant bhongale -20-7-16Angular js animate shashikant bhongale -20-7-16
Angular js animate shashikant bhongale -20-7-16Shashikant Bhongale
 
14_ HTML Con't.ppt
14_ HTML Con't.ppt14_ HTML Con't.ppt
14_ HTML Con't.pptvasujaiswal4
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS PresentationShawn Calvert
 
Intro to MVC 3 for Government Developers
Intro to MVC 3 for Government DevelopersIntro to MVC 3 for Government Developers
Intro to MVC 3 for Government DevelopersFrank La Vigne
 

Similar to Knockoutjs Part 4 Bindings Controlling text and appearance (20)

Knockout.js
Knockout.jsKnockout.js
Knockout.js
 
Knockoutjs
KnockoutjsKnockoutjs
Knockoutjs
 
Fundaments of Knockout js
Fundaments of Knockout jsFundaments of Knockout js
Fundaments of Knockout js
 
Knockoutjs Part 5 Bindings - Control flow
Knockoutjs Part 5 Bindings - Control flowKnockoutjs Part 5 Bindings - Control flow
Knockoutjs Part 5 Bindings - Control flow
 
Knockoutjs databinding
Knockoutjs databindingKnockoutjs databinding
Knockoutjs databinding
 
MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2MVC and Razor - Doc. v1.2
MVC and Razor - Doc. v1.2
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library
 
Web technologies part-2
Web technologies part-2Web technologies part-2
Web technologies part-2
 
Introduction to Web Technologies PPT.pptx
Introduction to Web Technologies PPT.pptxIntroduction to Web Technologies PPT.pptx
Introduction to Web Technologies PPT.pptx
 
Adding a view
Adding a viewAdding a view
Adding a view
 
Pfnp slides
Pfnp slidesPfnp slides
Pfnp slides
 
How to keep working technically interesting
How to keep working technically interestingHow to keep working technically interesting
How to keep working technically interesting
 
Organize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS TricksOrganize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS Tricks
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
 
Angular js animate shashikant bhongale -20-7-16
Angular js animate shashikant bhongale -20-7-16Angular js animate shashikant bhongale -20-7-16
Angular js animate shashikant bhongale -20-7-16
 
14_ HTML Con't.ppt
14_ HTML Con't.ppt14_ HTML Con't.ppt
14_ HTML Con't.ppt
 
Silverlight Databinding
Silverlight DatabindingSilverlight Databinding
Silverlight Databinding
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
 
Intro to MVC 3 for Government Developers
Intro to MVC 3 for Government DevelopersIntro to MVC 3 for Government Developers
Intro to MVC 3 for Government Developers
 
Email dssign rules
Email dssign rulesEmail dssign rules
Email dssign rules
 

More from Bhaumik Patel

1 - Introduction of Azure DevOps
1 - Introduction of Azure DevOps1 - Introduction of Azure DevOps
1 - Introduction of Azure DevOpsBhaumik Patel
 
Difference between MVC 3, 4, 5 and 6
Difference between MVC 3, 4, 5 and 6Difference between MVC 3, 4, 5 and 6
Difference between MVC 3, 4, 5 and 6Bhaumik Patel
 
Knockoutjs Part 3 Computed Observables
Knockoutjs Part 3 Computed ObservablesKnockoutjs Part 3 Computed Observables
Knockoutjs Part 3 Computed ObservablesBhaumik Patel
 
Knockoutjs Part 2 Beginners
Knockoutjs Part 2 BeginnersKnockoutjs Part 2 Beginners
Knockoutjs Part 2 BeginnersBhaumik Patel
 

More from Bhaumik Patel (6)

1 - Introduction of Azure DevOps
1 - Introduction of Azure DevOps1 - Introduction of Azure DevOps
1 - Introduction of Azure DevOps
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
Difference between MVC 3, 4, 5 and 6
Difference between MVC 3, 4, 5 and 6Difference between MVC 3, 4, 5 and 6
Difference between MVC 3, 4, 5 and 6
 
Knockoutjs Part 3 Computed Observables
Knockoutjs Part 3 Computed ObservablesKnockoutjs Part 3 Computed Observables
Knockoutjs Part 3 Computed Observables
 
Knockoutjs Part 2 Beginners
Knockoutjs Part 2 BeginnersKnockoutjs Part 2 Beginners
Knockoutjs Part 2 Beginners
 
HTML5 - Quick Guide
HTML5 - Quick GuideHTML5 - Quick Guide
HTML5 - Quick Guide
 

Knockoutjs Part 4 Bindings Controlling text and appearance

  • 2. Knockout’s declarative binding system provides a concise and powerful way to link data to the UI. Binding syntax Today's message is: <span data-bind="text: myMessage"></span> A binding consists of two items, the binding name and value, separated by a colon. Here is an example of a single, simple binding:
  • 3. 1. The visible binding 2. The text binding 3. The html binding 4. The css binding 5. The style binding 6. The attr binding
  • 4.  Purpose • The visible binding causes the associated DOM element to become hidden or visible according to the value you pass to the binding. Example <div data-bind="visible: shouldShowMessage"> You will see this message only when "shouldShowMessage" holds a true value. </div> <script type="text/javascript"> var viewModel = { shouldShowMessage: ko.observable(true) // Message initially visible }; viewModel.shouldShowMessage(false); // ... now it's hidden viewModel.shouldShowMessage(true); // ... now it's visible again </script>
  • 5.  Purpose • The text binding causes the associated DOM element to display the text value of your parameter Example Today's message is: <span data-bind="text: myMessage"></span> <script type="text/javascript"> var viewModel = { myMessage: ko.observable() // Initially blank }; viewModel.myMessage("Hello, world!"); // Text appears </script>
  • 6.  Purpose • The html binding causes the associated DOM element to display the HTML specified by your parameter. Example <div data-bind=“html: details “></div> <script type="text/javascript"> var viewModel = { details : ko.observable() }; viewModel. details(“<em>Hello, world!<em>”); </script>
  • 7.  Purpose • The css binding adds or removes one or more named CSS classes to the associated DOM element. This is useful, for example, to highlight some value in red if it becomes negative. Example <div data-bind=“css: { profitWarning: currentProfit() < 0 }“> Profit Information </div> <script type="text/javascript"> var viewModel = { currentProfit : ko.observable(20000) }; viewModel.currentProfit(-15); </script>
  • 8.  Purpose • The style binding adds or removes one or more style values to the associated DOM element. This is useful, for example, to highlight some value in red if it becomes negative, or to set the width of a bar to match a numerical value that changes. Example <div data-bind=“style: { color: currentProfit() < 0 ? 'red' : 'black' }“> Profit Information </div> <script type="text/javascript"> var viewModel = { currentProfit : ko.observable(20000) }; viewModel.currentProfit(-15); </script>
  • 9.  Purpose • The attr binding provides a generic way to set the value of any attribute for the associated DOM element. Example <a data-bind=“attr: { href: url, title: details}“> Report </a> <script type="text/javascript"> var viewModel = { url: ko.observable("year-end.html"), details: ko.observable("Report including final year-end statistics") }; </script>
  • 10. You see, God helps only people who work hard. That principle is very clear. - A. P. J. Abdul Kalam