Skip to content

Commit e2493f6

Browse files
author
Dean Sofer
committed
Crappy screwing around with the code. Why does adding scope:anything break it?
1 parent 56c0f1d commit e2493f6

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

demo/index.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818
</head>
1919
<body ng-controller="MainCtrl">
2020

21+
<p>
22+
Selected: {{data.custom}}
23+
</p>
24+
2125
<ui-select ng-model="data.custom">
2226
<li
23-
ng-repeat="item in data.items | filter : $search"
24-
ng-class="{highlight:highlight==$index}"
25-
ng-click="$select(item)"
26-
ng-mouseover="$parent.highlight=$index">
27+
ng-repeat="item in data.items | filter : $select.search"
28+
ng-class="{highlight:$select.index==$index}"
29+
ng-click="$select(item.title)"
30+
ng-mouseover="$select.index=$index">
2731
<h4>{{item.title}}</h4>
2832
</li>
2933
</ui-select>
3034

35+
3136
</body>
3237
</html>

select.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ angular.module('ui.select', ['ui.keypress']).directive('uiSelect', function(){
33
restrict: 'E',
44
/* jshint multistr: true */
55
template: '<div class="select" ng-class="{open:open}"> \
6-
<input type="{{type}}" ui-keydown="{up: \'up()\', down: \'down()\'}" ng-model="$search" ng-click="activate()"> \
6+
<input type="{{type}}" ui-keydown="{up: \'up()\', down: \'down()\', enter: \'$select((data.items|filter: $select.search)[$select.index].title)\'}" ng-model="$select.search" ng-click="activate()"> \
77
<ul ng-transclude></ul> \
88
</div>',
99
replace: true,
1010
require: 'ngModel',
1111
transclude: true,
12+
// scope: true,
1213
link: function($scope, $elm, $attrs, ngModel){
1314

1415
//Setting keybindings to scope wasn't working, since ui-keydown directive
@@ -22,30 +23,33 @@ angular.module('ui.select', ['ui.keypress']).directive('uiSelect', function(){
2223
$scope.type = 'text';
2324
input.focus();
2425
};
25-
$scope.$watch('$search', function(){
26-
$scope.highlight = 0;
26+
$scope.$watch('$select.search', function(){
27+
$scope.$select.index = 0;
2728
});
2829
$scope.up = function(){
29-
if ($scope.highlight > 0)
30-
$scope.highlight--;
30+
if ($scope.$select.index > 0)
31+
$scope.$select.index--;
3132
};
3233
$scope.down = function(){
3334
items = $elm.find('ul').children().length;
34-
if ($scope.highlight < items) {
35-
$scope.highlight++;
35+
if ($scope.$select.index < items) {
36+
$scope.$select.index++;
3637
} else {
37-
$scope.highlight = items;
38+
$scope.$select.index = items;
3839
}
3940
};
40-
$scope.select = function(item){
41+
$scope.$select = function(item){
4142
ngModel.$setViewValue(item);
43+
ngModel.$render(item);
44+
$scope.close();
45+
};
46+
$scope.close = function() {
47+
$scope.open = false;
48+
$scope.type = 'button';
4249
};
4350
ngModel.$render = function(){
44-
$scope.$search = ngModel.$viewValue;
51+
$scope.$select.search = ngModel.$viewValue;
4552
};
46-
input.bind('blur', function(){
47-
$scope.$apply('open=false;type="button"');
48-
});
4953
}
5054
};
5155
});

0 commit comments

Comments
 (0)