JSFiddle - React, Tailwind, and code Playground
by Shaun Scovil
HTML
<div ng-controller="MyController as vm">
<my-select callback="vm.setValue($value)"></my-select>
<p>Selected Value: {{vm.value}}</p>
</div>
JavaScript
angular.module('myApp', [])
.controller('MyController', MyController)
.controller('MySelectController', MySelectController)
.directive('mySelect', mySelectDirective)
;
function MyController() {
var vm = this;
vm.setValue = function(value) {
vm.value = value;
};
}
function MySelectController() {
var vm = this;
vm.items = [
{label: 'One', value: 1},
{label: 'Two', value: 2},
{label: 'Three', value: 3}
];
}
function mySelectDirective() {
return {
bindToController: {
callback: '&'
},
controller: 'MySelectController',
controllerAs: 'vm',
link: postLink,
scope: true,
template: '<select ng-model="vm.selected" ng-options="item.value as item.label for item in vm.items"></select>'
};
function postLink(scope, iElement, iAttrs, vm) {
scope.$watch(function() {
return vm.selected;
}, function(newValue) {
if (angular.isDefined(newValue) && typeof vm.callback === 'function') {
vm.callback({$value: newValue});
}
});
}
}