Knockout.js 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

with

with绑定来重新定义一个上下文绑定,如下代码:

<h1 data-bind="text: city"> </h1> 
 <p data-bind="with: coords"> 
     Latitude: <span data-bind="text: latitude"> </span>, 
     Longitude: <span data-bind="text: longitude"> </span> 
 </p>
ko.applyBindings({
         city: "London",
         coords: {
             latitude: 51.5001524,
             longitude: -0.1262362
         }
     });

这样在使用coordslatitudelongitude的时候就不需要使用coords.latitude来调用了,因为我们使用with:coords来指定了coords的上下文,当我们使用coords下面的属性时就可以直接使用了。

将上面的代码在编辑环境中去试一试。