1

With the help of the official Angular-Material Docs found here, I have managed to add three autocomplete fields next to each other. However, I would like to bind the value selected from the first dropdown field to be automatically selected on the third dropdown field too.

What would be the best approach to binding the first autocomplete value to the third but NOT vice-versa?

The autocomplete of all three fields looks like this but each of them are wrapped in their own separate controllers:

<form ng-submit="$event.preventDefault()"> <md-autocomplete ng-disabled="ctrl.isDisabled" md-no-cache="ctrl.noCache" md-selected-item="ctrl.selectedItem" md-search-text-change="ctrl.searchTextChange(ctrl.searchText)" md-search-text="ctrl.searchText" md-selected-item-change="ctrl.selectedItemChange(item)" md-items="item in ctrl.querySearch(ctrl.searchText)" md-item-text="item.display" md-min-length="1" md-floating-label="Final"> <md-item-template> <span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span> </md-item-template> <md-not-found> No items matching "{{ctrl.searchText}}" were found. </md-not-found> </md-autocomplete> </form> 

And the JS which I copied from the example Docs:

app.controller('abcCtrl', abcCtrls); function abcCtrls ($timeout, $q, $log) { var self = this; self.simulateQuery = false; self.isDisabled = false; // list of `state` value/display objects self.states = loadAll(); self.querySearch = querySearch; self.selectedItemChange = selectedItemChange; self.searchTextChange = searchTextChange; self.newState = newState; function newState(state) { alert("Sorry! You'll need to create a Constituion for " + state + " first!"); } // ****************************** // Internal methods // ****************************** /** * Search for states... use $timeout to simulate * remote dataservice call. */ function querySearch (query) { var results = query ? self.states.filter( createFilterFor(query) ) : self.states, deferred; if (self.simulateQuery) { deferred = $q.defer(); $timeout(function () { deferred.resolve( results ); }, Math.random() * 1000, false); return deferred.promise; } else { return results; } } function searchTextChange(text) { $log.info('Text changed to ' + text); } function selectedItemChange(item) { $log.info('Item changed to ' + JSON.stringify(item)); } /** * Build `states` list of key/value pairs */ function loadAll() { var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\ Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\ Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\ Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\ North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\ South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\ Wisconsin, Wyoming'; return allStates.split(/, +/g).map( function (state) { return { value: state.toLowerCase(), display: state }; }); } /** * Create filter function for a query string */ function createFilterFor(query) { var lowercaseQuery = angular.lowercase(query); return function filterFn(state) { return (state.value.indexOf(lowercaseQuery) === 0); }; } }; 

1 Answer 1

1

md-selected-item-change is an expression to be run each time a new item is selected

So in your function selectedItemChange, set the model which represents third autocompletefield.

HTML

<div ng-app="myApp" ng-controller="abcCtrl as ctrl"> <form ng-submit="$event.preventDefault()"> <md-autocomplete ng-disabled="ctrl.isDisabled" md-no-cache="ctrl.noCache" md-selected-item="ctrl.selectedItem" md-search-text-change="ctrl.searchTextChange(ctrl.searchText)" md-search-text="ctrl.searchText" md-selected-item-change="ctrl.selectedItemChange(item)" md-items="item in ctrl.querySearch(ctrl.searchText)" md-item-text="item.display" md-min-length="1" md-floating-label="Final"> <md-item-template><span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span> </md-item-template> <md-not-found>No items matching "{{ctrl.searchText}}" were found.</md-not-found> </md-autocomplete> <md-autocomplete ng-disabled="ctrl.isDisabled" md-no-cache="ctrl.noCache" md-selected-item="ctrl.selectedItem2" md-search-text-change="ctrl.searchTextChange(ctrl.searchText2)" md-search-text="ctrl.searchText2" md-items="item in ctrl.querySearch(ctrl.searchText2)" md-item-text="item.display" md-min-length="1" md-floating-label="Final"> <md-item-template><span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span> </md-item-template> <md-not-found>No items matching "{{ctrl.searchText}}" were found.</md-not-found> </md-autocomplete> <md-autocomplete ng-disabled="ctrl.isDisabled" md-no-cache="ctrl.noCache" md-selected-item="ctrl.selectedItem3" md-search-text-change="ctrl.searchTextChange(ctrl.searchText3)" md-search-text="ctrl.searchText3" md-items="item in ctrl.querySearch(ctrl.searchText3)" md-item-text="item.display" md-min-length="1" md-floating-label="Final"> <md-item-template><span md-highlight-text="ctrl.searchText3" md-highlight-flags="^i">{{item.display}}</span> </md-item-template> <md-not-found>No items matching "{{ctrl.searchText3}}" were found.</md-not-found> </md-autocomplete> </form> </div> 

JavaScript:

var app = angular.module('myApp', ['ngMaterial']); app.controller('abcCtrl', abcCtrls); function abcCtrls($timeout, $q, $log) { var self = this; self.simulateQuery = false; self.isDisabled = false; // list of `state` value/display objects self.states = loadAll(); self.querySearch = querySearch; self.selectedItemChange = selectedItemChange; self.searchTextChange = searchTextChange; self.newState = newState; function newState(state) { alert("Sorry! You'll need to create a Constituion for " + state + " first!"); } // ****************************** // Internal methods // ****************************** /** * Search for states... use $timeout to simulate * remote dataservice call. */ function querySearch(query) { var results = query ? self.states.filter(createFilterFor(query)) : self.states, deferred; if (self.simulateQuery) { deferred = $q.defer(); $timeout(function () { deferred.resolve(results); }, Math.random() * 1000, false); return deferred.promise; } else { return results; } } function searchTextChange(text) { $log.info('Text changed to ' + text); } function selectedItemChange(item) { self.selectedItem3 = item; $log.info('Item changed to ' + JSON.stringify(item)); } /** * Build `states` list of key/value pairs */ function loadAll() { var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\ Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\ Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\ Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\ North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\ South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\ Wisconsin, Wyoming'; return allStates.split(/, +/g).map(function (state) { return { value: state.toLowerCase(), display: state }; }); } /** * Create filter function for a query string */ function createFilterFor(query) { var lowercaseQuery = angular.lowercase(query); return function filterFn(state) { return (state.value.indexOf(lowercaseQuery) === 0); }; } } 
Sign up to request clarification or add additional context in comments.

7 Comments

"So in your function selected-Item-Change, set the model which represents third autocomplete field" So I should add the above function below or above the current js that I copied Rayon?
Btw, I'm just starting to learn angularJs so please don't mind the noob questions.
Just set the value of third element model i.e. selectedItem
So this md-selected-item-change="ctrl.selectedItemChange(item)" will be md-selected-item-change="newState" or?
See ctrl.selectedItemChange(item) is a method which is being called and it has argument as selected value. Just set this value as third element model. Do not complicate things...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.