[AngularJS] $scope.$watch

本文介绍如何使用AngularJS实现密码强度检查,并通过实时反馈帮助用户改善密码输入,确保安全性。
/**
 * Created by Answer1215 on 11/13/2014.
 */

function MainCtrl($scope){

    function isLongEnough (pwd) {
        return pwd.length > 4;
    }

    function hasNumbers (pwd) {
        return /[0-9]/.test(pwd);
    }

    this.watchPwd =  function(newVal, oldVal){
        //first init, you might got undefined
        if (!newVal) return;
        $scope.reqs = [];

        if (!isLongEnough(newVal)) {
            $scope.reqs.push('Too short');
        }

        if (!hasNumbers(newVal)) {
            $scope.reqs.push('Must include numbers');
        }

        $scope.showReqs = $scope.reqs.length;
    }

    //user.password is a string
    //user is an object
    $scope.$watch('user.password',this.watchPwd);

    //$watch can accept the third argument, once add it, angularJS
    //will do lose value checking instead of reference checking, it's quite
    //expensive
    //in all $watch is expensive, use ng-change if you can
   // $scope.$watch('user.password',this.watchPwd, true);
}

angular.module('app',[])
    .controller('MainCtrl', MainCtrl);

$watch can accept the third arguement, once set it as true:

$scope.$watch('user.password',this.watchPwd, true);

AngularJS will do lose value checking instead of reference checking, it is quite expensive.

For best pratise: avoid using $watch as much as you can, instead using ng-change.

<input ng-model="myModel" ng-change="callback">
<!--
  $scope.callback = function () {
    // go
  };
-->

 

<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
    <meta charset="UTF-8">
    <title>$watch</title>
    <script src="bower_components/angular/angular.min.js"></script>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
    <style type="text/css">
        .panel {
            width: 70%;
            margin: 30px auto;
        }
    </style>
</head>
<body ng-controller="MainCtrl">

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Create Account</h3>
    </div>
    <div class="panel-body">
        <form role="form">
            <div class="form-group">
                <label for="eml">Email address</label>
                <input id="eml"
                        type="email"
                        class="form-control"
                        placeholder="Email address"
                        ng-model="user.email"/>
            </div>

            <div class="form-group">
                <label for="pwd">Password</label>
                <input id="pwd"
                       type="password"
                       class="form-control"
                       placeholder="Password"
                       ng-model="user.password" />
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
        </form>
    </div>
</div>
<div class="panel panel-danger" ng-show="showReqs">
    <div class="panel-heading">
        <h3 class="panel-title">Password Requirement</h3>
    </div>
    <div class="panel-body">
        <ul>
            <li ng-repeat="req in reqs">{{req}}</li>
        </ul>
    </div>
</div>
<script src="app.js"></script>
</body>
</html>

 

Read More: https://egghead.io/lessons/angularjs-the-basics-of-scope-watch

转载于:https://www.cnblogs.com/Answer1215/p/4093988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值