Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here we simulated async call factory by using wrapper with <code>$q</code>.</p> <ul> <li>We changed <code>modelReflist</code> to <code>modelRefList</code></li> <li>added <code>ng-model="item"</code> to template</li> </ul> <p><strong>HTML</strong></p> <pre><code>&lt;div ng-controller="TestCtrl"&gt; &lt;input-select model-ref-list="countryList"&gt;&lt;/input-select&gt; &lt;/div&gt; </code></pre> <p><strong>JS</strong></p> <pre><code>var App = angular.module('myModule', ['ngResource']); App.controller( 'TestCtrl', [ '$scope', 'countriesFactory', function ($scope, countriesFactory) { /* Call API */ countriesFactory.resourceAPICall().then(function (data) { $scope.countryList = data.country; console.log($scope.countryList); }); }]) App.$inject = ['$scope', 'countriesFactory']; App.directive("inputSelect", function () { var Template = '&lt;select ng-model="item" ng-options="item.label as item.label for item in modelRefList" required&gt;&lt;/select&gt;'; return { restrict: 'EA', template: Template, scope: { modelRefList: '=' }, link: function (scope) { console.log(scope.countryList); } }; }); App.factory('countriesFactory', ['$resource', '$q', function ($resource, $q) { var data = { "country": [{ "code": "ABW", "label": "Aruba" }, { "code": "AFG", "label": "Afghanistan" }, { "code": "AGO", "label": "Angola" }] }; var factory = { resourceAPICall: function () { var deferred = $q.defer(); deferred.resolve(data); return deferred.promise; } } return factory; }]); </code></pre> <p>Demo <kbd><strong><a href="http://jsfiddle.net/9Ymvt/836/" rel="noreferrer">Fiddle</a></strong></kbd></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload