gpt4 book ai didi

angularjs - md-chips 不与 md-autocomplete 一起使用

转载 作者:行者123 更新时间:2023-12-02 22:41:24 25 4
gpt4 key购买 nike

我正在尝试处理这个片段,其中自动完成功能嵌入在芯片中。但从自动完成中选择的项目不会转换为筹码。

自动完成的数据采用以下方式:{name:"John Doe", id:"1"}

哪里错了,请指教。

问候

这是我的芯片代码:

<md-chips ng-model="student_ex" 
md-autocomplete-snap
md-transform-chip="transformChip($chip)"
md-require-match flex>
<md-autocomplete flex
md-selected-item="student"
md-search-text="searchText"
md-items="item in searchStudent(searchText)"
md-item-text="item.name"
placeholder="Search for a Student to Exclude">
<span md-highlight-text="searchText">ID: {{ item.id }} | Name: {{ item.name }}</span>
</md-autocomplete>
<md-chip-template>
<span>
<strong>{{$chip}}</strong><em></em>
</span>
</md-chip-template>
</md-chips>

这是我的searchStudent 和transformChip:

$scope.searchStudent = function (query) {
if ((/^\d+$/.test(query))) {
var results = query ? $scope.student_list.filter(
function (name) {
var regex = new RegExp(query,'gi');
return name.id.match(regex);
}
) : $scope.student_list;
} else {
var results = query ? $scope.student_list.filter(
function (name) {
var lowercaseQuery = angular.lowercase(query);
var regex = new RegExp(lowercaseQuery,'gi');
return name.name.match(regex);
}
) : $scope.student_list;
}
return results;
};

$scope.transformChip = function (chip) {
return {items:chip};
}

最佳答案

初始化模型,如下所示:

$scope.student_ex = [];

angular.module('MyApp', ['ngMaterial']).controller('AppCtrl', function($scope) {
$scope.student_list = $scope.student_list = [{name:"John Doe", id:"1"}, {name:"Antipod", id:"2"}];
$scope.student_ex = [];

$scope.searchStudent = function (query) {
if ((/^\d+$/.test(query))) {
var results = query ? $scope.student_list.filter(
function (name) {
var regex = new RegExp(query,'gi');
return name.id.match(regex);
}
) : $scope.student_list;
} else {
var results = query ? $scope.student_list.filter(
function (name) {
var lowercaseQuery = angular.lowercase(query);
var regex = new RegExp(lowercaseQuery,'gi');
return name.name.match(regex);
}
) : $scope.student_list;
}
return results;
};

$scope.transformChip = function (chip) {
return chip;
}
});
<link rel="stylesheet" href="https://material.angularjs.org/latest/angular-material.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>

<script src="https://material.angularjs.org/latest/angular-material.min.js"></script>

<div ng-controller="AppCtrl" ng-cloak="" ng-app="MyApp">
<md-chips ng-model="student_ex"
md-autocomplete-snap
md-transform-chip="transformChip($chip)"
md-require-match flex>
<md-autocomplete flex
md-selected-item="student"
md-search-text="searchText"
md-items="item in searchStudent(searchText)"
md-item-text="item.name"
placeholder="Search for a Student to Exclude">
<span md-highlight-text="searchText">ID: {{ item.id }} | Name: {{ item.name }}</span>
</md-autocomplete>
<md-chip-template>
<span>
<strong>{{$chip.name}}</strong><em></em>
</span>
</md-chip-template>
</md-chips>
</div>

关于angularjs - md-chips 不与 md-autocomplete 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38764863/

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