In my opinion, you have several options (as indicated in the comments):
1. create a controller for the directive
Use the controller in the directive template, i.e.
<div ng-controller="SelectController"> </div>
and create a SelectController as a regular controller:
var app = angular.module("app.controllers", []) app.controller("SelectController", ['$scope', 'myDataService', function(scope, service) { scope.options = service.whatEverYourServiceDoesToProvideThis() }]);
You can also give your directive a controller that works the same way:
recManagerApp.directive(myDirective, function () { return { restrict: 'E', templateUrl: '/templates/directives/mydirective.html', scope: { mySelectedValue: "=", }, controller: ['$scope', 'myDataService', function(scope, service) { scope.options = service.whatEverYourServiceDoesToProvideThis() }] }; });
2. introducing it into a directive and using it in a link
recManagerApp.directive(myDirective, function (myDataService) { return { restrict: 'E', templateUrl: '/templates/directives/mydirective.html', scope: { mySelectedValue: "=" }, link: function(scope) { scope.options = myDataService.whatEverYourServiceDoesToProvideThis() } }; });
source share