第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

angularjs中的密碼檢查指令

angularjs中的密碼檢查指令

慕村225694 2019-12-16 10:05:33
我正在寫(xiě)一個(gè)密碼驗(yàn)證指令: Directives.directive("passwordVerify",function(){    return {        require:"ngModel",        link: function(scope,element,attrs,ctrl){            ctrl.$parsers.unshift(function(viewValue){                var origin = scope.$eval(attrs["passwordVerify"]);                if(origin!==viewValue){                    ctrl.$setValidity("passwordVerify",false);                    return undefined;                }else{                    ctrl.$setValidity("passwordVerify",true);                    return viewValue;                }            });        }    };});html:<input data-ng-model='user.password' type="password" name='password' placeholder='password' required><input data-ng-model='user.password_verify' type="password" name='confirm_password' placeholder='confirm password' required data-password-verify="user.password">給定一個(gè)格式的2個(gè)密碼字段,如果兩個(gè)密碼值相等,則該指令影響的字段有效。問(wèn)題是它以一種方式起作用(即,當(dāng)我在密碼驗(yàn)證字段中鍵入密碼時(shí))。但是,當(dāng)原始密碼字段更新時(shí),密碼驗(yàn)證無(wú)效。知道如何進(jìn)行“雙向綁定驗(yàn)證”嗎?angularjs中的密碼檢查指令
查看完整描述

4 回答

?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

這應(yīng)該解決它:


視圖:


<div ng-controller='Ctrl'>

   <form name='form'>

      <input data-ng-model='user.password' type="password" name='password' placeholder='password' required>

      <div ng-show="form.password.$error.required">

        Field required</div>

      <input ng-model='user.password_verify' type="password" name='confirm_password' placeholder='confirm password' required data-password-verify="user.password">

      <div ng-show="form.confirm_password.$error.required">

        Field required!</div>

      <div ng-show="form.confirm_password.$error.passwordVerify">

        Fields are not equal!</div>

   </form

</div>

指示


var app = angular.module('myApp', []);


app.directive("passwordVerify", function() {

   return {

      require: "ngModel",

      scope: {

        passwordVerify: '='

      },

      link: function(scope, element, attrs, ctrl) {

        scope.$watch(function() {

            var combined;


            if (scope.passwordVerify || ctrl.$viewValue) {

               combined = scope.passwordVerify + '_' + ctrl.$viewValue; 

            }                    

            return combined;

        }, function(value) {

            if (value) {

                ctrl.$parsers.unshift(function(viewValue) {

                    var origin = scope.passwordVerify;

                    if (origin !== viewValue) {

                        ctrl.$setValidity("passwordVerify", false);

                        return undefined;

                    } else {

                        ctrl.$setValidity("passwordVerify", true);

                        return viewValue;

                    }

                });

            }

        });

     }

   };

});


查看完整回答
反對(duì) 回復(fù) 2019-12-16
?
qq_笑_17

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊

我使用以下指令,因?yàn)槲蚁胫匦买?yàn)證兩個(gè)輸入字段,而不管值1或值2是否更改:


指示:


'use strict';


angular.module('myApp').directive('equals', function() {

  return {

    restrict: 'A', // only activate on element attribute

    require: '?ngModel', // get a hold of NgModelController

    link: function(scope, elem, attrs, ngModel) {

      if(!ngModel) return; // do nothing if no ng-model


      // watch own value and re-validate on change

      scope.$watch(attrs.ngModel, function() {

        validate();

      });


      // observe the other value and re-validate on change

      attrs.$observe('equals', function (val) {

        validate();

      });


      var validate = function() {

        // values

        var val1 = ngModel.$viewValue;

        var val2 = attrs.equals;


        // set validity

        ngModel.$setValidity('equals', ! val1 || ! val2 || val1 === val2);

      };

    }

  }

});

用法


<input type="password" ng-model="value1" equals="{{value2}}" required>

<input type="password" ng-model="value2" equals="{{value1}}" required>


查看完整回答
反對(duì) 回復(fù) 2019-12-16
?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

不需要為此創(chuàng)建單獨(dú)的指令。Angular UI密碼驗(yàn)證工具中已經(jīng)存在一個(gè)內(nèi)部版本。這樣,您可以執(zhí)行以下操作:


<input name="password" required ng-model="password">

<input name="confirm_password"

       ui-validate=" '$value==password' "

       ui-validate-watch=" 'password' ">


 Passwords match? {{!!form.confirm_password.$error.validator}}


查看完整回答
反對(duì) 回復(fù) 2019-12-16
?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

對(duì)此的另一種做法是使一個(gè)輸入的模型與另一個(gè)輸入的值匹配。


app.directive('nxEqual', function() {

? ? return {

? ? ? ? require: 'ngModel',

? ? ? ? link: function (scope, elem, attrs, model) {

? ? ? ? ? ? if (!attrs.nxEqual) {

? ? ? ? ? ? ? ? console.error('nxEqual expects a model as an argument!');

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? scope.$watch(attrs.nxEqual, function (value) {

? ? ? ? ? ? ? ? model.$setValidity('nxEqual', value === model.$viewValue);

? ? ? ? ? ? });

? ? ? ? ? ? model.$parsers.push(function (value) {

? ? ? ? ? ? ? ? var isValid = value === scope.$eval(attrs.nxEqual);

? ? ? ? ? ? ? ? model.$setValidity('nxEqual', isValid);

? ? ? ? ? ? ? ? return isValid ? value : undefined;

? ? ? ? ? ? });

? ? ? ? }

? ? };

});

因此,如果密碼盒的型號(hào)為,login.password則在驗(yàn)證盒上設(shè)置以下屬性:nx-equal="login.password",并測(cè)試formName.elemName.$error.nxEqual。像這樣:


<form name="form">

? ? <input type="password" ng-model="login.password">

? ? <input type="password" ng-model="login.verify" nx-equal="login.password" name="verify">

? ? <span ng-show="form.verify.$error.nxEqual">Must be equal!</span>

</form>

擴(kuò)大的視野:


對(duì)于我的一個(gè)新項(xiàng)目,我必須修改上述指令,以使其僅nxEqual在驗(yàn)證輸入具有值時(shí)才顯示錯(cuò)誤。否則nxEqual錯(cuò)誤應(yīng)被忽略。這是擴(kuò)展版本:


app.directive('nxEqualEx', function() {

? ? return {

? ? ? ? require: 'ngModel',

? ? ? ? link: function (scope, elem, attrs, model) {

? ? ? ? ? ? if (!attrs.nxEqualEx) {

? ? ? ? ? ? ? ? console.error('nxEqualEx expects a model as an argument!');

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? scope.$watch(attrs.nxEqualEx, function (value) {

? ? ? ? ? ? ? ? // Only compare values if the second ctrl has a value.

? ? ? ? ? ? ? ? if (model.$viewValue !== undefined && model.$viewValue !== '') {

? ? ? ? ? ? ? ? ? ? model.$setValidity('nxEqualEx', value === model.$viewValue);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? });

? ? ? ? ? ? model.$parsers.push(function (value) {

? ? ? ? ? ? ? ? // Mute the nxEqual error if the second ctrl is empty.

? ? ? ? ? ? ? ? if (value === undefined || value === '') {

? ? ? ? ? ? ? ? ? ? model.$setValidity('nxEqualEx', true);

? ? ? ? ? ? ? ? ? ? return value;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? var isValid = value === scope.$eval(attrs.nxEqualEx);

? ? ? ? ? ? ? ? model.$setValidity('nxEqualEx', isValid);

? ? ? ? ? ? ? ? return isValid ? value : undefined;

? ? ? ? ? ? });

? ? ? ? }

? ? };

});

您將像這樣使用它:


<form name="form">

? ? <input type="password" ng-model="login.password">

? ? <input type="password" ng-model="login.verify" nx-equal-ex="login.password" name="verify">

? ? <span ng-show="form.verify.$error.nxEqualEx">Must be equal!</span>

</form>


查看完整回答
反對(duì) 回復(fù) 2019-12-16
  • 4 回答
  • 0 關(guān)注
  • 778 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)