【发布时间】:2016-06-13 23:33:06
【问题描述】:
好的,第一个问题,所以......善良..
我正在使用 Angular 的 Material 框架制作这个 Angular Autofill 输入。
我需要用来自 ajax 调用的数据填充它。 (即一个国家arrey) 我最初的方法是将模块注入应用程序的控制器中,然后当 ajax 调用返回成功时,我会触发它的函数。
这不能很好地工作,因为它在数据返回之前加载一次(从注入)并在检查时崩溃。
采取两个:添加对空数据的检查导致它按预期加载和停止,然后当数据到达时(最终)模块将它们分成单独的项目并准备好但页面无法工作。不会工作我的意思是它不会在你输入时自动建议..
我认为我需要加载模块而不是其中的函数,但我似乎无法做到这一点..这就是我需要帮助的..
添加代码:
angular.module("Myapp", ['ngMaterial', 'Autofill-country' ])
.controller("mycontroller", ["$scope", "$http", function ($scope, $http) {
//automatic suggest
automaticsuggest ()
function automaticsuggest() {
suggesturl = 'http://www.mysuggest_front_services'
$http({
method: 'GET',
url: suggesturl,
responseType: "json",
async: false // Just tried that to see if it works.. I am not really fond of it
}).then(function (response) {
countriesarrey = response.data.Countries
// .run('Autofill-country', ['Autofill-country']) - Tried this too..
country(); // this is the function of the autosuggest.
Once checked with breakpoints it does get the array and cut it and filter it etc, but the n when you input something in the input field.. It doesnt suggest..
});
}
还有自动完成模块
angular.module('Autofill-country', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
.controller('country', country);
function country($timeout, $q) {
var self = this;
if (countriesarrey == "undefined" || countriesarrey == null) { }
else { countrys(); }
// list of `country` value/display objects
function countrys() {
self.countries = loadAll();
self.selectedItem = null;
self.searchTextcountry = null;
self.querySearch = querySearch;
// ******************************
// Internal methods
// ******************************
/**
* Search for countries... use $timeout to simulate
* remote dataservice call.
*/
function querySearch(query) {
var results = query ? self.countries.filter(createFilterFor(query)) : [];
return results;
}
/**
* Build `countries` list of key/value pairs
*/
function loadAll() {
var allcountries = countriesarrey;
console.log(countriesarrey);
return allcountries.split(/, +/g).map(function (country) {
return {
value: country.toLowerCase(),
display: country
};
});
}
/**
* Create filter function for a query string
*/
function createFilterFor(query) {
var lowercaseQuery = angular.lowercase(query);
return function filterFn(country) {
return (country.value.indexOf(lowercaseQuery) === 0);
};
}
}
}
【问题讨论】:
标签: javascript jquery angularjs ajax inject