I'm new to AngularJS. I'm trying to create my first directive. I'm having some challenges. In an attempt to "learn-by-doing", I decided to create a custom HTML element via a directive. My element will allow a user to choose a day of the week. My control template looks like the following:
myControl.tpl.html
<select>
<option value="1">Sunday</option>
<option value="2">Monday</option>
<option value="3">Tuesday</option>
<option value="4">Wednesday</option>
<option value="5">Thursday</option>
<option value="6">Friday</option>
<option value="7">Saturday</option>
</select>
myControl.js
angular.module('myControls.dayPicker', [])
.directive('myDayPicker', function () {
return {
restrict: 'E',
scope: { value: '=' },
templateUrl: 'dayPicker/myControl.tpl.html',
controller: function ($scope) {
}
};
});
This directive should be reusable across views. To use it, I want to be able to enter the following code:
The selectedDay value will be a value that is on the $scope in my controller. For some reason, I can't seem to get two way binding to work. How do I make it so:
- The selected day option value gets set to the selectedDay $scope property?
- How do I automatically set the correct option if the selectedDay is already set?
Thank you!