gpt4 book ai didi

javascript - 如果选择了自动完成输入,则显示/隐藏 Div

转载 作者:行者123 更新时间:2023-11-29 21:39:23 26 4
gpt4 key购买 nike

我有两个 Angular-Material 自动完成字段添加为 shown here到目前为止,它运行良好。

自动完成代码:

HTML:

<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.searchText2)"
md-search-text="ctrl.searchText2"
md-selected-item-change="ctrl.selectedItemChange(item)"
md-items="item in ctrl.querySearch(ctrl.searchText2)"
md-item-text="item.display"
md-min-length="1"
md-floating-label="Destination"
ng-model="destiFlight">
<md-item-template>
<span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
</md-item-template>
<md-not-found>
No states matching "{{ctrl.searchText}}" were found.
</md-not-found>
</md-autocomplete>
</form>

JS:

.controller('someCtrl', someCtrl);
function someCtrl($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);
};
}
};

现在我在两个字段下面都添加了一个 div,如下所示:

<div class="someDiv">
....
</div>

只有从自动完成下拉列表中选择了一个值时,如何才能显示 div?

最佳答案

您应该使用存储自动完成所选值的模型:

自动完成:

<md-autocomplete
md-selected-item="mySelectedItem"
md-items="item in myList"
md-item-text="item.name"
md-min-length="0"
placeholder="Select country">

和 div:

<div class="someDiv" ng-show="mySelectedItem && mySelectedItem.length != 0">
....
</div>

关于javascript - 如果选择了自动完成输入,则显示/隐藏 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com