问题描述:

I've made my own validation directive to check that two fields cannot be zero at the same time. When I pass to the directive attribute the variable which contains the other value (the number inside the other field) in order to check them, it's always the value which I used to initializethe var. The directive is getting the value properly because if I put manually any random number or even a string, the directive gets that value correctly.

You can see how I've tried to print those variable contains after each label in order to see their values, and they are printing the correct value, but the directive still get the initial value in both cases. Also, inside the directive I'm printing the values of the both variables in the console to check them and is there where I always see that attr.zeroTimeDirective always is zero (the initial value). And as I said if I send "hello world" to the directive (zero-time-validation="hello world") the var attr.zeroTimeDirective is just hello world.

My HTML:

<div>

<md-button class="md-raised md-primary" ng-click="addDateField()" style="margin-left: 50px">Add day</md-button>

<div ng-repeat="day in overtimeList">

<md-list-item layout-lg="row" layout-xl="row" style="margin-top: 0">

<md-datepicker ng-model="day.date" md-min-date="today" md-placeholder="Before to" style="padding-top: 1px" name="OTdate" ></md-datepicker>

<md-input-container style="margin-left: 30px">

<label>Hours*</label> <!--{{day.minutes}}-->

<md-select ng-model="day.hours" name="hours" zero-time-directive="{{day.minutes}}">

<md-option ng-repeat="hour in time.hours" value="{{hour}}">

{{hour}}

</md-option>

</md-select>

</md-input-container>

<md-input-container style="margin-left: 30px">

<label>Minutes*</label><!--{{day.hours}}-->

<md-select ng-model="day.minutes" name="minutes" zero-time-directive="{{day.hours}}">

<md-option ng-repeat="minute in time.minutes" value="{{minute}}">

{{minute}}

</md-option>

</md-select>

</md-input-container>

</md-list-item>

</div>

</div>

My custom validation directive:

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

return{

restrict: 'A',

require: 'ngModel',

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

var otherSideValue = attr.zeroTimeDirective;

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

console.log("selected: "+value+"|| otherside: "+otherSideValue);

if (value == 0 && otherSideValue == 0)

ctrl.$setValidity('noZero', false);

else

ctrl.$setValidity('noZero', true);

return value;

});

}

};

});

The controller:

function overtimeHoursController($scope){

function overtime(){

this.date = new Date();

this.hours = 0;

this.minutes = 0;

}

$scope.today = moment(new Date()).subtract(1, "days").toDate();

$scope.dateSelected = new Date();

$scope.time = {

hours : [0, 1, 2, 3, 4, 5, 6, 7, 8],

minutes : [0, 15, 30, 45],

selectedHour: 0,

selectedMinute: 0

}

$scope.overtimeList = [new overtime()];

$scope.addDateField = function(){

$scope.overtimeList.push(new overtime());

}

}

相关阅读:
Top