I am new to angular js custom directive and i have created the following directive for 3 way toggle switch as below with html and css.
JS Code
(function () {
"use strict";
var directiveId = "myToggle";
angular.module("myApp").directive(directiveId, [function () {
return {
restrict: 'E',
template:
'<div class="ng-toggle-switch">'+
' <input type="radio" class="ng-toggle-switch-input" name="view" id="selected" checked>'+
' <label for="selected" class="ng-toggle-switch-label">selected</label>'+
' <input type="radio" class="ng-toggle-switch-input" name="view" id="unselected1">'+
' <label for="unselected1" class="ng-toggle-switch-label">unselected</label>'+
' <input type="radio" class="ng-toggle-switch-input" name="view" id="unselected2" >'+
' <label for="unselected2" class="ng-toggle-switch-label">unselected</label>'+
'</div>',
scope:{
items:'=',
selectedValue:'='
},
link: function(scope, element, attributes){
}
}
}]);
})();
HTML
<my-toggle></my-toggle>
CSS
.ng-toggle-switch {
position: relative;
width: 100%;
border: 1px solid #0098F3;
max-width: 323px;
height: 34px;
border-radius: 4px;
}
.ng-toggle-switch-label {
float: left;
text-align: center;
cursor: pointer;
padding-left: 16px !important;
line-height: 34px;
border-right: 1px solid #0098F3;
padding-right: 16px;
color: #0098F3;
}
.ng-toggle-switch-label:last-child
{
border: none;
}
.ng-toggle-switch-input {
display: none;
}
.ng-toggle-switch-input:checked + .ng-toggle-switch-label {
background: #0098F3;
color: #fff;
}
What i have created is pretty much static. there will be only 3 buttons for now with their static values. I need to make it dynamic so that it can be used across various apps. here i need the person who is going to use this directive should be able to pass number of buttons needed and the value selected. any advise will be appreciated.
Thanks in advance.
