2014-02-20 12 views
5

Ich habe ein Formular, in dem es 2 Datumsfelder gibt, von date und todate. Ich schreibe eine Anweisung, um dieses Datumsfeld zu vergleichen.Angularjs + Wie man einen vorherigen Eingabefeld Wert in Direktive nimmt

Wie nehme ich das from date filed.Ich berufe meine benutzerdefinierte Direktive auf onBlur von toDate an und muss jetzt fromdate nehmen. Das Formular Design ist wie fillows.

<div class="row"> 
    <div class="col-xs-6 col-sm-6 col-6"> 
    <label>From date</label> 
    <input type="text" id="eduFromDate" name="eduFromDate" class="form-control" 
      ng-model="education.fromDate" ui-date/> 
    </div> 
    <div class="col-xs-6 col-sm-6 col-6"> 
    <label>To date</label> 
    <input type="text" name="edutoDate" class="form-control" id="edutoDate" 
      ng-model="education.toDate" ui-date before-date/> 
    <span class="error input-icon fui-alert" 
      ng-show="historyForm.edutoDate.$error.beforeDate"></span> 
    </div> 
</div> 
+0

hilft vielleicht diese [Plunker] (http://plnkr.co/edit/bTFMzV70vjOzwhimVtQL?p=preview) gegeben. Möchten Sie einen Datumsbereich berechnen? –

+0

möglich duplicate von [Custom form validation Direktive, um zwei Felder zu vergleichen] (http://stackoverflow.com/questions/20982751/custom-form-validation-directive-to-compare-two-fields) – Stewie

+0

@stewie Ihr Link hat mir geholfen viel bei der Entwicklung der Richtlinie. – Vishnupriya

Antwort

0

Mein Richtcode ist wie folgt.

directives.directive('beforeDate', [ 
    function() { 

     var link = function($scope, $element, $attrs, ctrl) { 

      var validate = function(viewValue) { 
       var comparisonModel = $attrs.beforeDate; 
       console.log(new Date(comparisonModel)); 
       if(!viewValue || !comparisonModel){ 
        ctrl.$setValidity('beforeDate', true); 
       } 
       ctrl.$setValidity('beforeDate', new Date(viewValue) > new Date(comparisonModel)); 
       return viewValue; 
      }; 

      ctrl.$parsers.unshift(validate); 
      ctrl.$formatters.push(validate); 

      $attrs.$observe('beforeDate', function(comparisonModel){ 
       console.log('comparisonModel '+comparisonModel) 
       return validate(ctrl.$viewValue); 
      }); 

     }; 

     return { 
      require: 'ngModel', 
      link: link 
     }; 

    } 
]); 

und in der Form habe ich wie

<span ng-repeat="education in userEducation"> 
    <div class="row"> 
    <div class="col-xs-6 col-sm-6 col-6"> 
     <label>From date</label> 
     <input type="text" id="eduFromDate" name="eduFromDate" class="form-control" ng-model="education.fromDate" ui-date> 
    </div> 
    <div class="col-xs-6 col-sm-6 col-6"> 
     <label>To date</label> 
     <div class="form-group has-success"> 
     <input type="text" name="edutoDate" class="form-control" id="edutoDate" ng-model="education.toDate" ui-date before-date="eduFromDate"> 
     <span class="error input-icon fui-alert" ng-show="historyForm.edutoDate.$error.beforeDate"></span> 
     </div> 
    </div> 
    </div> 
</span> 
Verwandte Themen