Skip to content

Instantly share code, notes, and snippets.

@joelbyler
Created April 9, 2014 02:10
Show Gist options
  • Save joelbyler/10219412 to your computer and use it in GitHub Desktop.
Save joelbyler/10219412 to your computer and use it in GitHub Desktop.
Angular.js + jQuery + Harvest/Chosen + Custom Directive
<script>
var app = angular.module('myApp', []);
var myController = app.controller('myController', ['$scope', function ($scope, $timeout) {}]);
myController.directive('chosen', function(){
var linker = function(scope, element, attrs, ngModel) {
var model = attrs['ngModel'];
scope.$watch(model, function () {
$(element).trigger("chosen:updated");
});
$(element).addClass('chosen-select');
$(element).chosen({disable_search_threshold: 10}).change(function(){
ngModel.$setViewValue($(this).val());
scope.$apply();
console.log(ngModel.$modelValue);
});
};
return {
restrict:'A',
require:'ngModel',
link: linker
}
})
</script>
<body>
<div ng-app="myApp" ng-controller="myController">
<select ng-model="number" style="width:200px">
<option value="1" selected>A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
</div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment