我已经为 Angular UI typeahead 字段制定了指令。我正在尝试设计它,以便当用户键入时,它会发送异步后端调用以获取结果,这些结果将填充出现的下拉列表,如 Angular-bootstrap docs, example 2 中所示。 。但是,当我开始输入(在本例中为“a”)时,我收到错误:
TypeError: Cannot use 'in' operator to search for 'getters' in a
这是进行 REST 调用的工厂方法:
certFactory.getUsersWithMatchingUsername = function(username) {
return $http.get(urlBase + '/managed/user?_queryFilter=userName+co+' + '\'' + username + '\'', {timeout: timeoutLength})
.then(function(response) {
// success
return response.data;
}, function(response) {
// error
return $q.reject(response);
});
};
这是调用工厂方法的 Controller 方法:
$scope.getters = {
getUsersWithUsername: function (username) {
certFactory.getUsersWithMatchingUsername(username)
.then(function (response) {
var users = [];
angular.forEach(response.result, function(item) {
users.push(item);
})
return users;
}, function (error) {
console.log('failed!')
})
}
这是我的指令:
angular.module('myApp')
.directive('dropdownsearch', function(){
return {
restrict: 'E',
transclude: true,
scope: {
getterFn: '&',
config: '=', // object with properties label and type
disabled: '=?ngDisabled',
required: '=?ngRequred',
ngModel: '=',
options: '='
},
require: ['^form', 'ngModel'],
templateUrl: 'views/templates/dropdownSearchView.html',
replace: true,
link: function(scope, iElm, iAttrs, controller) {
if (iAttrs.required !== undefined) {
// If attribute required exists
// ng-required takes a boolean
scope.required = true;
}
if (iAttrs.readonly !== undefined) {
// If attribute required exists
// ng-required takes a boolean
scope.readonly = true;
}
}
}
}
);
这是指令模板:
<div class="form-group has-feedback">
<label class="control-label"> Choose {{ config.type }}></label>
<div class="dropdown dropdown">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="Make selection"
ng-model="ngModel"
uib-typeahead="option as option[config.label] for option in getterFn($viewValue)"
typeahead-editable="false"
ng-required="required"
ng-disabled="disabled"
/>
</div>
</div>
</div>
最后,这是我的使用指令:
<dropdownsearch ng-show= 'fieldMethods.showSubfield(subfield)'
getter-fn="getters.getUsersWithUsername"
ng-model="subsubfield.value"
config="fieldMethods.getConfig(subfield)">
</dropdownsearch>
任何帮助将不胜感激。另外,如果需要任何其他信息,请告诉我。
最佳答案
UI Bootstrap 网站异步 Typeahead 示例使用 uib-typeahead="address for address in getLocation($viewValue)"
。我的猜测是,您的错误消息表明它期望指令中的 for
,而不是 as
。我不明白这一点,所以我可能是错的! :-)
关于javascript - Angular typeahead 异步结果 - 在“运算符中接收错误 "TypeError: Cannot use ',以在...中搜索...”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187702/