Angular material 将默认值设为md自动完成
如何在md自动完成中传递默认值 图1:HTML代码 图2:JS代码 图3:输出 正如你所看到的,我没有得到任何默认国家作为输出。有办法吗Angular material 将默认值设为md自动完成,angular-material,Angular Material,如何在md自动完成中传递默认值 图1:HTML代码 图2:JS代码 图3:输出 正如你所看到的,我没有得到任何默认国家作为输出。有办法吗 为搜索文本指定默认值并选择编辑对象 $scope.local ={ ... searchText : 'Default Value', selectedItem : 'Default object' ... } 我用超时来做这个 $timeout(function() { $scope.local =
为搜索文本指定默认值并选择编辑对象
$scope.local ={
...
searchText : 'Default Value',
selectedItem : 'Default object'
...
}
我用超时来做这个
$timeout(function() {
$scope.local = {selectedItem : 1}
}, 2000);
我使用自动完成和默认值编写small。
你必须做的是:
$scope.local = {
...
selectedItem: 1, // Must be object, but not integer
...
}
(功能(A){
“严格使用”;
var app=A.module('app',['ngMaterial']);
主要功能(
$q,
$scope,
$timeout
) {
$timeout(函数(){
$scope.user={
名字:“马克西姆”,
姓:“Dunaevsky”,
组:{
id:1,
标题:“管理员”
}
};
}, 500);
$scope.loadGroups=函数(filterText){
变量d=$q.defer(),
allItems=[{
id:1,
标题:“管理员”
}, {
id:2,
标题:“经理”
}, {
id:3,
标题:“主持人”
}, {
id:4,
标题:“VIP用户”
}, {
id:5,
标题:“标准用户”
}];
$timeout(函数(){
var项目=[];
A.forEach(所有项目、功能(项目){
if(项目标题索引(过滤器文本)>-1){
项目。推送(项目);
}
});
d、 解决(项目);
}, 1000);
回报承诺;
};
}
main.$inject=[
“$q”,
“$scope”,
“$timeout”
];
应用控制器(“主”,主);
}(这是有角度的)代码>
形式
名字
姓
{{item.title}
没有物品。
模型为JSON
{{user | json}}
我知道这是一个老问题,但有些人可能会从我的解决方案中受益。我一直在努力解决自动完成的模型是异步的,并且将自动完成作为ng重复的一部分。在web上找到的许多解决方案只有一个自动完成的静态数据
我的解决方案是向autocomplete添加另一个指令,并在我想要设置为autocomplete默认值的变量上加一个手表
在我的模板中:
<md-autocomplete initscope='{{quote["Scope"+i]}}' ng-repeat='i in [1,2,3,4,5,6,7,8]'
class='m-1'
md-selected-item="ScopeSelected"
md-clear-button="true"
md-dropdown-position="top"
md-search-text="pScopeSearch"
md-selected-item-change='selectPScope(item.label,i)'
md-items="item in scopePSearch(pScopeSearch,i)"
md-item-text="item.label"
placeholder="Plowing Scope {{i}}"
md-min-length="3"
md-menu-class="autocomplete-custom-template"
>
}))
这将检查对引号[“Scope”+i]的更改(因为最初它将为null),并创建初始选定项,并将自动完成的选定项设置为该对象。然后它将一个初始化值设置为true,这样就不会再发生这种情况
Details.directive('initscope', function () {
return function (scope, element, attrs) {
scope.$watch(function (){
return attrs.initscope;
}, function (value, oldValue) {
//console.log(attrs.initscope)
if(oldValue=="" && value!="" && !scope.initialized){
//console.log(attrs.initscope);
var item = {id:0, order:0,label:attrs.initscope?attrs.initscope:"" }
scope.ScopeSelected = item;
scope.initialized = true;
}
});
};