我正在尝试为loadAllOriginal创建一个类似的javascript函数。 但是变量allEmployees的值是一个带有employeesoyee对象的列表(ID,FirstName,LastName)。我该怎么做? 我试图使用此方法与此页面中的自动完成: https://material.angularjs.org/latest/demo/autocomplete
function loadAllOriginal()
{
var allEmployees = '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 allEmployees.split(/, +/g).map(function (employee)
{
return
{
value: employee.toLowerCase(),
display: employee
};
});
}
function loadAll()
{
var allEmployees = $http.post('../Admin/GetUsers/');
..........
}
答案 0 :(得分:0)
好的,所以我假设您知道如何将记录加载到allEmployees变量中。我已经更新了我的答案,包括如何从异步函数调用中获取数据,然后能够使用该数据。
通过使用延迟对象并从loadAll()函数返回promise,可以使其工作的一种方法。这可以使用内置为angular的$ q服务来完成。这是实现它的一种方法:
function loadAll() {
return $q(function (resolve, reject) {
$http.post('../Admin/GetUsers/').then(function successCallback(response) {
resolve(response.data.map(function (employee, index) {
return {
value: employee.ID,
display: employee.FirstName + " " + employee.LastName
}
}));
}, function failureCallback(response) {
reject("Error!");
});
});
}
var allEmployees = [];
loadAll().then(function success(employees) {
allEmployees = employees;
}, function failure(message) {
console.log(message);
allEmployees = [];
}).then(function (){
console.log(allEmployees);
// rest of your code that uses the allEmployees variable goes here...
});