I am trying to pass down an object into a directive that can then update this value. so far i have the following:
<competence-select ng-model="module.selectedCompetence"></competence-select>
Directive
angular.module('Competence').directive("competenceSelect", ['competenceService', function (competenceService) {
return {
restrict: "E",
templateUrl: 'js/modules/Competence/directives/competence-select/competence-select.html',
require: 'ngModel',
link: function (scope, element, attr, ngModel) {
ngModel.$setViewValue = scope.competenceList;
scope.competences = [];
competenceService.getCompetenceList().then(function (result) {
scope.competences = result;
})
}
};
}]);
(Notice the require)
And then my directive html:
<label translate="FORMS.COMPETENCES"></label>
<ui-select multiple reset-search-input="true" ng-model="competenceList" theme="bootstrap"
ng-disabled="disabled">
<ui-select-match placeholder="{{ 'FORMS.COMPETENCES_PLACEHOLDER' | translate }}">{{$item.name}}
<{{$item.competence_type_id == 1 ? 'Faglig' : 'Personlig' }}></ui-select-match>
<ui-select-choices group-by="someGroupFn"
repeat="competence in competences | propsFilter: {name: $select.search, competence_type_id: $select.search}">
<div ng-bind-html="competence.name | highlight: $select.search"></div>
<small>
{{competence.name}}
{{ 'COMPETENCES.TYPE' | translate:{TYPE: competence.competence_type_id} }}
</small>
</ui-select-choices>
</ui-select>
Now what i want to do is fairly simple:
ngModel$setViewValue = scope.competence;
Setting this should set the ng-model in the view to be the ng-model i set on the directive. Thereby parsing the variable "up" to the declarating of the directive:
<competence-select ng-model="module.selectedCompetence"></competence-select>
Sadly this is not the case.
Can anyone tell me what ive done wrong?
Make these changes in your directive
angular.module('Competence').directive("competenceSelect", ['competenceService', function (competenceService) {
return {
restrict: "E",
templateUrl: 'js/modules/Competence/directives/competence-select/competence-select.html',
require: 'ngModel',
scope:{ ngModel : "=" },
link: function (scope, element, attr, ngModel) {
ngModel.$setViewValue = scope.competenceList;
scope.competences = [];
competenceService.getCompetenceList().then(function (result) {
scope.competences = result;
scope.ngModel = scope.competences;
})
}
};
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With