I've been attempting to pass dynamic variables into a directive that showcases a time picker (using pickadate.js: ). However, I'm facing challenges in getting these options into the directive. Despite searching extensively online, I am overwhelmed by the different approaches available and struggling to make it work correctly. Below is my current code:
Directive:
// Directive for selecting time on an HTML element
appDirectives.directive('pickATime', function() {
return {
// Attribute restriction
restrict: 'A',
// Link function for updating the DOM
link: function(scope, element, attrs) {
element.pickatime(scope.$eval(attrs.pickATime));
},
scope: {
timeOptions: '=options'
},
templateUrl: '../Templates/timeoptions.html'
};
});
Directive Template:
Min: {{timeOptions.startTime}} Max: {{customerInfo.endTime}}
HTML:
<input type="text" placeholder="Start Time" id="timestart" pick-a-time options="timeRange" data-ng-model="itemtimestart" class="form-control" autocomplete="off">
Controller with dynamic values from REST:
// Query settings from REST
appSettings.query(function(settings) {
var setting = settings.value;
angular.forEach(setting, function(settingvalue, settingkey) {
if (settingvalue.Title == 'Active') {
workDuration = settingvalue.Work_x0020_Day_x0020_Hours_x0020;
nonWorkDuration = settingvalue.Non_x0020_Work_x0020_Day_x0020_H;
var startHour = settingvalue.Work_x0020_Day_x0020_Start_x0020.split(":")[0];
var startMinute = settingvalue.Work_x0020_Day_x0020_Start_x0020.split(":")[1];
var startTime = '[' + startHour + ',' + startMinute + ']';
var endHour = settingvalue.Work_x0020_Day_x0020_End_x0020_M.split(":")[0];
var endMinute = settingvalue.Work_x0020_Day_x0020_End_x0020_M.split(":")[1];
var endTime = '[' + endHour + ',' + endMinute + ']';
$scope.timeRange = {min: startTime, max: endTime};
}
})
});
A static method working on an input:
<input type="text" placeholder="End Time" id="timeend" pick-a-time="{min: [0,0], max: [23,30]}" data-ng-model="itemtimeend"class="form-control" autocomplete="off">
Update: After adjusting with Dave's help, the timeRange logs correctly but timeOptions in the directive shows as undefined.
Directive (timeOptions undefined in logging):
appDirectives.directive('pickATime', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.pickatime(scope.pickATime);
console.log("time options log" + scope.timeOptions);
},
scope: {
timeOptions: '='
},
templateUrl: '../Templates/timeoptions.html'
};
});
Template:
min: {{timeOptions.min}}, max: {{timeOptions.max}}
Controller (logging correctly):
// Get $scope variables to control time picker range
var startHour = settingvalue.Work_x0020_Day_x0020_Start_x0020.split(":")[0];
var startMinute = settingvalue.Work_x0020_Day_x0020_Start_x0020.split(":")[1];
var startTime = '[' + startHour + ',' + startMinute + ']';
var endHour = settingvalue.Work_x0020_Day_x0020_End_x0020_M.split(":")[0];
var endMinute = settingvalue.Work_x0020_Day_x0020_End_x0020_M.split(":")[1];
var endTime = '[' + endHour + ',' + endMinute + ']';
$scope.timeRange = {
min: startTime,
max: endTime
};
HTML:
<input type="text" placeholder="Start Time" id="timestart" pick-a-time timeOptions="timeRange" data-ng-model="itemtimestart" class="form-control" autocomplete="off">
Update - Picker Working... but Now Form is not Submitting Time Data: details can be found in this Post: TimePicker directive won't submit time (undefined)
Special thanks to @dave-alperovich and @joe-enzminger for their invaluable assistance and insightful responses.
Controller:
// Get $scope variables to control time picker range
var startHour = settingvalue.Work_x0020_Day_x0020_Start_x0020.split(":")[0];
var startMinute = settingvalue.Work_x0020_Day_x0020_Start_x0020.split(":")[1];
var endHour = settingvalue.Work_x0020_Day_x0020_End_x0020_M.split(":")[0];
var endMinute = settingvalue.Work_x0020_Day_x0020_End_x0020_M.split(":")[1];
$scope.timeRange = {
min: [startHour,startMinute],
max: [endHour,endMinute]
};
Directive:
appDirectives.directive('pickATime', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.pickatime(scope.options());
},
scope: {
options: '&pickATime'
},
};
});
Usage:
<input ng-if="timeRange" type="text" placeholder="Start Time" id="timestart" pick-a-time="timeRange" data-ng-model="itemtimestart" class="form-control" autocomplete="off">