Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3237f8b

Browse files
committed
fix(directive): ng:options to support ng:change
Closes #463
1 parent 7802c90 commit 3237f8b

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Issue #449: [ng:options] should support binding to a property of an item.
66
- Issue #464: [ng:options] incorrectly re-grew options on datasource change
77
- Issue #448: [ng:options] should support iterating over objects
8+
- Issue #463: [ng:options] should support firing ng:change event
89

910
### Breaking changes
1011
- no longer support MMMMM in filter.date as we need to follow UNICODE LOCALE DATA formats.

src/widgets.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ angularWidget('select', function(element){
674674
this.directives(true);
675675
var isMultiselect = element.attr('multiple');
676676
var expression = element.attr('ng:options');
677+
var onChange = expressionCompile(element.attr('ng:change') || "").fnSelf;
677678
var match;
678679
if (!expression) {
679680
return inputWidgetSelector.call(this, element);
@@ -729,7 +730,10 @@ angularWidget('select', function(element){
729730
value = valueFn(tempScope);
730731
}
731732
}
732-
if (!isUndefined(value)) model.set(value);
733+
if (!isUndefined(value) && model.get() !== value) {
734+
onChange(scope);
735+
model.set(value);
736+
}
733737
scope.$tryEval(function(){
734738
scope.$root.$eval();
735739
});

test/widgetsSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,27 @@ describe("widget", function(){
859859
expect(scope.selected).toEqual(scope.values[1]);
860860
});
861861

862+
it('should fire ng:change if present', function(){
863+
createSelect({
864+
name:'selected',
865+
'ng:options':'value for value in values',
866+
'ng:change':'count = count + 1'});
867+
scope.values = [{name:'A'}, {name:'B'}];
868+
scope.selected = scope.values[0];
869+
scope.count = 0;
870+
scope.$eval();
871+
expect(scope.count).toEqual(0);
872+
873+
select.val('1');
874+
browserTrigger(select, 'change');
875+
expect(scope.count).toEqual(1);
876+
expect(scope.selected).toEqual(scope.values[1]);
877+
878+
browserTrigger(select, 'change');
879+
expect(scope.count).toEqual(1);
880+
expect(scope.selected).toEqual(scope.values[1]);
881+
});
882+
862883
it('should update model on change through expression', function(){
863884
createSelect({name:'selected', 'ng:options':'item.id as item.name for item in values'});
864885
scope.values = [{id:10, name:'A'}, {id:20, name:'B'}];

0 commit comments

Comments
 (0)