I am new to Angular and am still painfully enveloping myself in special directives.
I would like to reuse this HTML bit
<ui-select ng-model="model.selectedLanguages" multiple search-enabled="true" theme="select2" style="width: 300px;"> <ui-select-match placeholder="Pick one...">{{$item.name}}</ui-select-match> <ui-select-choices repeat="lang.id as lang in langs |filter: { name : $select.search }"> <div ng-bind-html="lang.name | highlight: $select.search" ></div> </ui-select-choices> </ui-select>
wrapping it in my custom directive:
<language-picker ng-model="model.selectedLanguages"/>
something like that:
app.directive('languagePicker', function() { return { template : '<ui-select ng-model="**PARENT NGMODEL**" multiple search-enabled="true" theme="select2" style="width: 300px;"><ui-select-match >{{$item.name}}</ui-select-match><ui-select-choices repeat="lang.id as lang in langs | filter: { name : $select.search }"><div ng-bind-html="lang.name | highlight: $select.search"></div></ui-select-choices></ui-select>', restrict : 'E', require : 'ngModel', replace : true .... }; });
But how to pass ngModel from my language-picker directive to ui-select ?
UPDATE
Using the suggestions below, I got it with ui-select, but the external model is not updated at all, see plnkr.co/edit/Y43dmMGIc5GxM9fLoNPW , probably because the content area of the child and the parent area remain unchanged?
UPDATE 2
I did it in a confusing way, which looks awful for me, because I have no idea why it "works" in the first place (see strange things happening in the controller):
app.directive('languagePicker', function(LanguageService) { return { templateUrl : 'LanguagePickerTpl.html', restrict : 'E', scope : { languages : '=' }, controller : function($scope, LanguageService) { console.log($scope); $scope.langs = LanguageService.get(); $scope.model = $scope; } }; })
:
<ui-select ng-model="model.languages" multiple search-enabled="true" theme="select2" style="width: 300px;"> <ui-select-match>{{$item.name}}</ui-select-match> <ui-select-choices repeat="lang.id as lang in langs | filter: { name : $select.search }"> <div ng-bind-html="lang.name | highlight: $select.search"></div> </ui-select-choices> </ui-select>
I would be very happy if someone could explain what is happening (a “working” example works here http://plnkr.co/edit/B53F9sc7UGkj0uxUpC17 )