I am trying to pass dynamic variables into a directive that displays a time picker (pickadate.js: http://amsul.ca/pickadate.js/time/). However, I am struggling on how to get the options into the directive. I am scoured the internet, but see a multitude of approaches, and am confused on how best to construct this as it is not working. Here is my current code:
Directive:
// Pick a date directive used as pick-a-time on HTML element
appDirectives.directive('pickATime', function() {
return {
// Restrict it to be an attribute in this case
restrict: 'A',
// responsible for registering DOM listeners as well as 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 to pass in dynamic values (stored in REST)
// Query settings for variables to be used later in function
appSettings.query(function(settings) {
// Data is within an object of "value", so this pushes the server side array into a variable
var setting = settings.value;
// Foreach result, where setting is equal to active get the apporpriate variables
angular.forEach(setting, function(settingvalue, settingkey) {
if (settingvalue.Title == 'Active') {
// Get work and non work durations as variables
workDuration = settingvalue.Work_x0020_Day_x0020_Hours_x0020;
nonWorkDuration = settingvalue.Non_x0020_Work_x0020_Day_x0020_H;
// 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};
}
})
});
The non-dynamic method (which works) on an input is as follows:
<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: Working with Dave, I have adjusted to the following. It logs the timeRange correctly, but gives an undefined for the timeOptions in the directive
timeRange Log:
time start is [8,30]time end is [17,0]
timeOption log:
time options log undefined
Directive (timeOptions undefined on logging):
appDirectives.directive('pickATime', function() {
return {
// Restrict it to be an attribute in this case
restrict: 'A',
// responsible for registering DOM listeners as well as updating the DOM
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 (logs out 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 per this Post: TimePicker directive won't submit time (undefined)**
Huge thanks to @dave-alperovich and @joe-enzminger for tireless help and great answers.
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 it to be an attribute in this case
restrict: 'A',
// responsible for registering DOM listeners as well as updating the DOM
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">