码迷,mamicode.com
首页 > Web开发 > 详细

knockout.js

时间:2020-06-06 01:14:15      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:取消   ext   his   input   pre   data-bind   bind   script   col   

knockout.js

1.创建数据模型和监控属性

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8" />
    <script type="application/javascript" src="../js/knockout-3.1.0.js"></script>
</head>
<body>

    <h1>姓名:<strong data-bind="text:personName"></strong></h1>
    <h2>年龄:<strong data-bind="text:personAge"></strong></h2>


    <input id="txtName" type="text" data-bind="value:personName">
    <input id="txAge" type="text" data-bind="value:personAge" />
    <br />

    <button onclick="cancelSubscribe()">取消订阅</button>
    
    <script>
        var subscription=null;
        var ViewModel1={
            personName:ko.observable("123"),//KO的双向绑定
            personAge:ko.observable()//
        };
        ko.applyBindings(ViewModel1);//激活KO
        //开启订阅
        subscription=ViewModel1.personAge.subscribe(function (newValue) {
            if(isNaN(newValue)){
                alert("请输入数字!");
                ViewModel1.personAge("");
            }
        });
        function cancelSubscribe() {
            if (subscription) {
                subscription.dispose();//取消订阅
            }
        }
    </script>
</body>
</html>

 

2.自动计算属性

2.1 Computed Observable

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta charset="UTF-8">
    <script type="application/javascript" src="../js/knockout-3.1.0.js"></script>
</head>
<body>
    firstName is <span data-bind="text:firstName"></span><br/>
    lastName is <span data-bind="text:lastName"></span><br />
    fullName is <span data-bind="text:fullName"></span><br/>
    <input type="text" data-bind="value:firstName" /><br/>
    <input type="text" data-bind="value:lastName" /><br />

    <script type="application/javascript">
        function viewModel() {
            var self =this;
            self.firstName = ko.observable("");
            self.lastName =ko.observable("");
            //this关键字 计算属性
            self.fullName = ko.computed(function () {
               return  self.firstName()+.+self.lastName();
            },self);
        }
        ko.applyBindings(viewModel);
    </script>
</body>
</html>

2.2 分解用户输入

<!DOCTYPE html>
<html lang="en">
<head>
    <title>分解用户输入</title>
    <meta charset="UTF-8">
    <script type="application/javascript" src="../js/knockout-3.1.0.js"></script>

</head>
<body>

    firstName is <span data-bind="text:firstName"></span><br/>
    lastName is <span data-bind="text:lastName"></span><br />
    fullName is <span data-bind="text:fullName"></span><br/>
    <input type="text" data-bind="value:firstName" /><br/>
    <input type="text" data-bind="value:lastName" /><br />
    <input type="text" data-bind="value:fullName" />

    <script type="application/javascript">
        function ViewModel() {
            var self =this;
            self.firstName = ko.observable("");
            self.lastName =ko.observable("");
            self.fullName=ko.computed({
                read: function () { //读取 显示
                    return this.firstName() + " " + this.lastName();        
                },
                write:function (value) { //输入的操作
                    var lastSpacePos = value.lastIndexOf(" ");
                    if(lastSpacePos>0){
                        this.firstName(value.substring(0, lastSpacePos));
                        this.lastName(value.substring(lastSpacePos + 1));
                    }
                },
                owner:self
            });

        }
        ko.applyBindings(ViewModel);

    </script>

</body>
</html>

2.3 值的转换(value 转换)

<!DOCTYPE html>
<html lang="en">
<head>
    <title>value值转换</title>
    <meta charset="UTF-8">
    <script type="application/javascript" src="../js/knockout-3.1.0.js"></script>
</head>
<body>

     数字是 <span data-bind="text:price "></span><br/>
     价格是 <input type="text" data-bind="value:formattedPrice" /><br/>

    <script>
        function viewModel() {
            var self=this;
            self.price = ko.observable(12.445);
            self.formattedPrice=ko.computed({
                read:function () {
                    return $ + self.price().toFixed(2);//
                },
                write:function (value) {
                    value = parseFloat(value.replace(/[^\.\d]/g, ""));//
                    self.price(isNaN(value) ? 0 : value);//
                },
                owner:self
            });
        }
        ko.applyBindings(viewModel);
    </script>

</body>
</html>

2.4 筛选和验证用户输入

<!DOCTYPE html>
<html lang="en">
<head>
    <title>筛选和验证用户输入</title>
    <meta charset="UTF-8">
    <script type="application/javascript" src="../js/knockout-3.1.0.js"></script>
</head>
<body>
    请输入新的价格:<input  id="txtNumber" type="text" data-bind="value:attemptedValue"/>
    <div data-bind="visible:!lastInputWasValid()">
        <font color="red">请输入数字!</font>
    </div>

    <script>
        function MyViewModel() {
            this.acceptedNumericValue = ko.observable(123);
            this.lastInputWasValid = ko.observable(true);//控制显示 true/false

            this.attemptedValue = ko.computed({//输入的值
                read: this.acceptedNumericValue,
                write: function (value) {
                    if (isNaN(value))
                        this.lastInputWasValid(false);
                    else {
                        this.lastInputWasValid(true);
                        this.acceptedNumericValue(value);
                    }
                },
                owner: this
            });
        }
        ko.applyBindings(new MyViewModel());
    </script>
</body>
</html>

 

knockout.js

标签:取消   ext   his   input   pre   data-bind   bind   script   col   

原文地址:https://www.cnblogs.com/youguess/p/13052968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!