I made a directive for a custom dropdown. Everything works as expected - I define all the options in my controller and pass them on to the directive. Directive generates the html and on change it passes on the selected value to my ngModel.
What I would like to improve is. That initially I just set the ngModel to either PUBLIC, PRIVATE or HIDDEN. Then the directive (already knows?) the icon and the value to display inside the dropdown. When selection is made my model is again just either PUBLIC, PRIVATE or HIDDEN not { name: 'Public', icon: 'globe', value: 'PUBLIC' }.
Directive:
.directive('sidPrivacyDropdown', function () {
return {
template: '<div class="input-group sid-privacy-dropdown">\
<ui-select ng-model="selectedValue" theme="bootstrap" search-enabled="false" ng-disabled="disabled">\
<ui-select-match>{{selectedValue.name}}</ui-select-match>\
<ui-select-choices repeat="option in selectOptions">\
<div><span class="icon icon-{{option.icon}}"></span>{{option.name}}</div>\
</ui-select-choices>\
</ui-select>\
</div>',
scope: {
selectedValue: '=',
selectOptions: '='
},
replace: true
};
});
Controller:
$scope.privacyOptions = [
{ name: 'Public', icon: 'globe', value: 'PUBLIC' },
{ name: 'Members', icon: 'group', value: 'MEMBERS' },
{ name: 'Hidden', icon: 'lock', value: 'HIDDEN' }
];
HTML:
<div sid-privacy-dropdown selected-value="edit.privacy.field" select-options="privacyOptions"></div>
Since I'm relatively new to Angular and directives I have no idea how or where should I modify the values. Eventually I want the ngModel to be just a constant.
Edit! Maybe this will help to better understand what I want:
Here is a Plunker, that does half of what I want it to do: Plunker. I can now just assign 'HIDDEN' as the initial value. And the directive takes the correct item from all the options and displays it. I can't get it to work the other way around. When the 'selectedObject' changes it should update the selectedValue.
edit.privacy.fieldto be eitherPUBLIC,MEMBERSorHIDDEN. When setting the initial value for theedit.privacy.fieldI want to set it to again eitherPUBLIC,MEMBERSorHIDDEN. Currently I have to assign it like this$scope.privacyOptions[0]. And when I pass it to the server, I want it's value to be a word, not an{ 'name:'Public', icon:'globe', value:'PUBLIC' }object. - Artur K.'PUBLIC' to { name: 'Public', icon: 'globe', value: 'PUBLIC' }and after that before I pass it on, fromObject to 'PUBLIC'- Artur K.