The purpose of the with binding
Using the with binding format is data-bind= "With:attribute", using the with binding will treat the subsequent property as a new context binding. All elements within the with binding are bound by that context.
Of course, with bindings can also be used with if or foreach bindings.
Example 1
<H1 data-bind= "text:city" >
In this case, the Coords monitoring property is directly bound with the with, and the internal properties of the Coords monitoring property are called directly inside it. This shows the characteristics of the with binding.
Example 2: An interactive example
This example uses the with binding to dynamically add and remove its bound value as null/undefined or null/undefined
UI Source code:
<form data-bind= "Submit:gettweets" >
Twitter Account:
<input data-bind= "Value:twittername"/>
<button type= "Submit" >get tweets</button>
</form>
<div data-bind= "with: Resultdata ">
View Model Source:
function Appviewmodel () {
var self = this;
Self.twittername = ko.observable (' @example ');
Self.resultdata = Ko.observable (); No Initial value
self.gettweets = function () {
var name = Self.twittername (),
simulatedresults = [
{te Xt:name + ' What a nice Day '},
{text:name + ' building some cool apps. '},
{text:name + ' Just saw a famous Celebrity eating lard. Yum. '}
];
Self.resultdata ({retrievaldate:new Date (), toptweets:simulatedresults});
Self.clearresults = function () {
self.resultdata (undefined);
}
}
Ko.applybindings (New Appviewmodel ());
Note: With container-free binding (virtual binding)
Like the virtual bindings of If or foreach, the with binding is the same. Use the <!--ko--> and <!--/ko-->.
<ul>
<li>header element</li>
<!--ko with:outboundflight-->
...
<!--/ko-->
<!--ko with:inboundflight-->
...
<!--/ko-->
</ul>
The above is a small set to introduce the Knockoutjs 3.X API Fourth chapter of the data control flow with binding, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!