I just came up with a directive that loads a drop-down list according to the list coming from an API call ($ resource).
Controller:
App.controller(
'TestCtrl', [
'$scope', 'countriesFactory',
function($scope, countriesFactory){
countriesFactory().then(function(data){
$scope.countryList = data;
});
}])
The return API returns:
{"country":[{"code":"ABW","label":"Aruba"},{"code":"AFG","label":"Afghanistan"},{"code":"AGO","label":"Angola"}]}
Template:
<input-select model-ref-list="countryList"></input-select>
Directive
App
.directive("inputSelect"
, function() {
var Template =
'<select ng-options="item.label for item in modelRefList" required></select>';
return {
restrict: 'EA',
template: Template,
scope: {
modelRefList: '='
},
link: function(scope){
console.log(scope.modelRefList);
}
};
}
);
First of all: I greatly simplified the general problem, so it looks like the directive is completely overloaded in this situation, but in the end it is not: D.
Problem: My .log console is always undefined.
I did some research and realized that I needed to play with promises in order to wait until my list of countries was actually provided by the directive. Therefore, I tried to change my controller and not use the result of the promise of the API request, but the resource itself:
New controller:
App.controller(
'TestCtrl', [
'$scope', 'countriesFactory',
function($scope, countriesFactory){
$scope.countryList = resourceAPICall();
}])
But still undefined: /.
( , , ) ?
ANGULARJS 1.2:
App
.directive("inputSelect"
, function() {
var Template =
'<select ng-options="item.label for item in modelRefList" required></select>';
return {
restrict: 'EA',
template: Template,
scope: {
modelRefList: '='
},
link: function(scope){
scope.modelRefList.$promise.then(function(data){
console.log(data);
}
};
}
);
API , .
.