• KnockOut -- 快速入门


    简单示例

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="utf-8">
    </head>
    <body>
    
    <!-- <span data-bind="text: personName"></span> -->
    <h3>Meal upgrades</h3>
    <p>Make your flight more bearable by selecting a meal to match your social and economic status.</p>
    Chosen meal:
    <select data-bind="	options: availableMeals,   
    					optionsText: 'mealName',
    				   	value: chosenMeal"></select>
    <p>
        You've chosen:
        <b data-bind="text: chosenMeal().description"></b>
        (price: <span data-bind='text: chosenMeal().extraCost'></span>)
        <br>
        <!-- formatPrice方法 -->
        (price: <span data-bind='text: formatPrice(chosenMeal().extraCost)'></span>)
    </p>
    
    </body>
    
    <script type="text/javascript" src="../knockout-3.5.0rc2.debug.js"></script>
    
    <script type="text/javascript">
    var availableMeals = [
        { mealName: 'Standard', description: 'Dry crusts of bread', extraCost: 0 },
        { mealName: 'Premium', description: 'Fresh bread with cheese', extraCost: 9.95 },
        { mealName: 'Deluxe', description: 'Caviar and vintage Dr Pepper', extraCost: 18.50 },
    ];
    
    var viewModel = {
        chosenMeal: ko.observable(availableMeals[0])   //ko.observable:UI可以监控(observe)
    };
    
    ko.applyBindings(viewModel); // 注意:ko. applyBindings需要在上述HTML之后应用才有效
    
    function formatPrice(price) {
        return price == 0 ? "Free" : "$" + price.toFixed(2);
    }
    </script>
    </html>
    

    监控属性(Observables)

    demo2-1.observable.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
      </head>
    
      <body>
        <!-- text绑定注册到自身 -->
        The name is <span data-bind="text: personName"></span>
        <br />The Age is
        <span data-bind="text: personAge"></span>
      </body>
      <script src="../knockout-3.5.0rc2.debug.js"></script>
      <script>
        var myViewModel = {
          personName: "Bob1",
          personAge: 123
        };
    
        myViewModel = {
          personName: ko.observable("Bob2"),
          personAge: ko.observable(123)
        };
        
        ko.applyBindings(myViewModel);
    
        myViewModel.personName("Mary").personAge(111);
        
      </script>
    </html>
    

    demo2-2.fullname.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
      </head>
    
      <body>
        <!-- text 绑定注册到自身 -->
        The name is <span data-bind="text: fullName"></span>
      </body>
      <script src="../knockout-3.5.0rc2.debug.js"></script>
      <script>
        var viewModel = {
          firstName: ko.observable("Bob"),
          lastName: ko.observable("Smith")
        };
    
        // 依赖监控属性(Dependent Observables)
        // 这些函数是一个或多个监控属性, 如果他们的依赖对象改变,他们会自动跟着改变。
        //computed == dependentObservable
        viewModel.fullName = ko.dependentObservable(function() {
          return this.firstName() + " " + this.lastName();
        }, viewModel);
    
        ko.applyBindings(viewModel);
      </script>
    </html>
    

    The name is Bob Smith

  • 相关阅读:
    About Spring
    execle导入后 数据 无刷新 显示在 输入页面
    前端小神龟 -- 分页导航栏
    如何在mysql命令窗口获取到程序正在执行的sql语句
    div+css(ul li)实现图片上文字下列表布局
    C#异常之(已有打开的与此 Command 相关联,已有打开的与此命令相关联的 DataReader,必须首先将它关闭。)
    台灣VR內容產業聯盟_技術推廣中心
    2014新的一年到来,祝大家新年快乐
    matlab 提取文件路径名称,用于实现遍历读取文件(我的目的是遍历图像)
    无监督学习一些算法的简要概括(一)-稀疏自编码
  • 原文地址:https://www.cnblogs.com/tangge/p/10264344.html
Copyright © 2020-2023  润新知