Let's say I use quite some dropdown choices (aka comboboxes) for countries in my application. To avoid repeating the same code over and over again, I want to create a directive for that.
However: Using following directive doesn't meet all my expectations (see below), while copy-pasting the template does meet all my expectations..
app.directive('countrydropdown', function($compile) {
return {
restrict: 'E', //attribute or element
scope: {
countryUri: '='
},
templateUrl: 'countrydropdown.html',
controller : function($scope) {
$scope.listItems = [
{name: 'Afghanistan', code: 'AF'},
{name: 'Åland Islands', code: 'AX'},
{name: 'Albania', code: 'AL'},
];
where my template is:
<div>
model (inside directive): {{countryUri}}
<ui-select ng-model="countryUri" theme="selectize" >
<ui-select-match placeholder="Select or search a country in the list...">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="'/api/countries/'+country.code as country in listItems | filter: $select.search">
<span ng-bind-html="country.name | highlight: $select.search"></span>
<small ng-bind-html="country.code | highlight: $select.search"></small>
</ui-select-choices>
</ui-select>
</div>
What I expect it to do:
- Changing the first combo, changes the model $scope.mydata.selectedCountry. This change should also affect / update the second combo
- Changing the second combo, changes the model $scope.mydata.selectedCountry. Here also, the first combo should be affected / updated
- Pressing the clear button should clear the selections in both comboboxes (because the clear button makes the model $scope.mydata.selectedCountry == null)
I must be doing someting wrong, but I can't find it. Se my example in this plukr: http://plnkr.co/edit/oF1R0F1udfiRulx5NvLO?p=preview
Note that making changes in the first combobox, everyting seems to work fine. (second combo updates fine) Once I make a selection on the second, the binding seems to be 'broken'
Any hints on this?