javascript - Knockout difference between template binding and control flow binding -
I have seen two ways to use a template in knockout:
Using it:
& lt; Ul Data-Bind = "Template: {name: 'templateName', foreach: tags}" & gt; & Lt; / Ul & gt; & Lt; Script id = "templateName" type = "text / html" & gt; & Lt; Li class = "tagItem" & gt; & Lt; Span data-bind = "text: name" & gt; & Lt; / Span & gt; & Lt; / Li & gt; & Lt; / Script & gt; and:
& lt; Ul data-bound = "foreach: tag" & gt; & Lt; Li class = "tagItem" & gt; & Lt; Span data-bind = "text: name" & gt; & Lt; / Span & gt; & Lt; / Li & gt; & Lt; / Ul & gt; Both have the same view modal: var viewModel = {tags: ko.observableArray ({{name: 'tag1'}, {name: ' As far as I think the result is the same, but the second
- write less
- is not this weird script tag
- different
- Easy to understand
So what's the reason for this first case? (The only thing I see now is that it is in another file
Hala matter is big plus resusability Imagine that you want to put such a list in a handful of pages and they should look the same in all of them. Do not think that you want to create a template in different file and see it through data-bind additionally Whatever dynamically you can like, Which it will be repeated when using the data-bind = "template: {name: 'templateName', foreach: tags}" should therefore be a second plus resilience . To expand that point, think of something else about the following example: Using simple binding without a template means that you must have the HTML plus javascript Binding code must be written: var viewModel = {tags: ko.observableArray ([In case of templates this is very easy because you have to determine which array to select in template: & lt; ul Data-Bind = "Template: {name: 'templ AteName ', foreign language: tag} "& lt; / ul & gt; & lt; ul data-bound =" template: {name:' templateName ', foreach: news} "& gt; & lt; / Ul> This will present two lists for different arrays, the benefit of it is that you write the javascript binding code as well as the repetitive HTML. This means that we can use the same system that does not even include tags. The only obstacle is that even those objects in those arrays Amil who have the name of the property.
Comments
Post a Comment