Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(select): add ng-change support
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmukler committed Feb 23, 2015
1 parent 35d5d75 commit f4ce10e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ angular.module('material.components.select', [
function SelectDirective($mdSelect, $mdUtil, $mdTheming) {
return {
restrict: 'E',
require: '?ngModel',
compile: compile
};

Expand Down Expand Up @@ -119,7 +120,7 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming) {

$mdTheming(element);

return function postLink(scope, element, attr) {
return function postLink(scope, element, attr, ngModel) {
element.on('click', openSelect);

element.on('keydown', openOnKeypress);
Expand Down Expand Up @@ -147,10 +148,9 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming) {
scope: scope.$new(),
template: selectTemplate,
target: element[0],
ngModel: ngModel,
hasBackdrop: true,
loadingAsync: attr.mdOnOpen ? scope.$eval(attr.mdOnOpen) : false
}).then(function() {
element.attr('aria-expanded', false);
});
});
}
Expand Down Expand Up @@ -474,6 +474,10 @@ function SelectProvider($$interimElementProvider) {

configureAria();

if (opts.ngModel) {
opts.selectEl.controller('mdSelectMenu').init(opts.ngModel);
}

if (opts.loadingAsync && opts.loadingAsync.then) {
opts.loadingAsync.then(function() {
scope.$$loadingAsyncDone = true;
Expand Down
22 changes: 18 additions & 4 deletions src/components/select/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ describe('<md-select-menu>', function() {
expect($rootScope.model).toBe(3);
}));

it('should support the ng-change event', inject(function($rootScope, $document) {
var changeCalled = false;
$rootScope.changed = function() {
changeCalled = true;
};

var selectEl = setupSelect('ng-model="myModel", ng-change="changed()"', [1, 2, 3]);
openSelect(selectEl);
waitForSelectOpen();
var menuEl = $document.find('md-select-menu');
menuEl.triggerHandler({
type: 'click',
target: menuEl.find('md-option')[1]
});
expect(changeCalled).toBe(true);
}));

it('should deselect old and select new on click', inject(function($rootScope) {
$rootScope.model = 3;
var el = setup('ng-model="$root.model"', [1,2,3]);
Expand Down Expand Up @@ -461,16 +478,13 @@ describe('<md-select-menu>', function() {

describe('aria', function() {
var el;
beforeEach(inject(function($mdUtil, $q) {
beforeEach(inject(function($mdUtil, $q, $document) {
el = setupSelect('ng-model="someModel"', [1, 2, 3]);
$mdUtil.transitionEndPromise = function() {
var deferred = $q.defer();
deferred.resolve();
return deferred.promise;
};
}));

afterEach(inject(function($document) {
var selectMenus = $document.find('md-select-menu');
selectMenus.remove();
}));
Expand Down

0 comments on commit f4ce10e

Please sign in to comment.