0

I have an time input , and a directive which I would like to have for custom validation. I would need to have both the start time and end time. So I pass two values using the models to the directive.

<input type="time" data-ng-model="activity.etime" data-ng-date="activity.stime, activity.etime" >

Here's my directive. When I try to access the second paramater, it gives me a undefined value ( first one is fine). Is this a wrong way to use pass two values to a directive?

app.directive('ngDate', function () {
    'use strict';
    return {
        require: 'ngModel',
        link: function (scope, element, attr, aCtrl) {              
            function myValidation(stime,etime) {        
                console.log(etime);               
            }
        }    
    };
});
2
  • The easy solution is to split validator into two validators max-date=... and min-date=... that can be used separately or in pair. The other way is to put data in attributes and use attr to read them or better to attr.$oberve them in that case you will have validate-date min-date="..." max-date="..." Commented May 27, 2016 at 12:16
  • 1
    What @csharpfolk said or you could also just pass in activity and pull out stime and etime from the object Commented May 27, 2016 at 12:18

1 Answer 1

1

You really should split them into two different parameter, or just pass an object that contains those two attributes. If you really want this, you could perhaps do:

<input type="time" data-ng-model="activity.etime" data-ng-date="{activity.stime, activity.etime}" >

And then you could probably access them like this:

function myValidation(time) {        
    console.log(time.activity.stime, time.activity.etime);               
}

Disclaimer: I haven't tested this.

Sign up to request clarification or add additional context in comments.

5 Comments

I just tried it, and it's not working. I'm getting this in the browser console :TypeError: Cannot read property 'stime' of undefined
Well like I said, I didnt test it lol. Can u do a console.log(time, activity) so we can see whats in the variable?
Time returns etime, activity returns activity is not defined
Yeah I tried to create a jsfiddle and test this out and didnt get to work. I'm still a bit confused with directives of type 'A' and how parameters are passed with ngModel...
Considering other alternatives , I'm trying the pass the sdate using attributes, using {{ sdate }} , but is unable to convert it into a date object because the format is altered when the expression is used. So I'm hoping to get this to work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.