我一直在尝试在anuglarjs工厂进行可重复使用的自动搜索。 这是我的代码。但不幸的是得到错误:“无法读取属性'然后'未定义'。试图在Google上搜索并在堆栈溢出但未能修复它。
(function() {
'use strict'
angular.module('App').factory('LoadFormActivity', ['$http', '$q', function($http, $q) {
function NGAutoCompleteSearch(url, val) {
var def = $q.defer();
$http.get(url, {
params: {
searchvalue: val
}
}).then(function(response) {
debugger
def.resolve(response);
}, function(response) {
debugger
def.reject(response);
});
return def.promise;
};
return {
NGAutoCompleteSearch: NGAutoCompleteSearch
};
}]);
})();
//Controller.
(function() {
'use strict'
angular.module('App').controller('ServicesMasterCtrl', ['$timeout', '$scope', '$filter', '$http', 'NGAutoComplete', 'LoadFormActivity', function($timeout, $scope, $filter, $http, NGAutoComplete, LoadFormActivity) {
$scope.FormService.AutoSearch = {};
$scope.FormService.AutoSearch.Search = function(id) {
LoadFormActivity.NGAutoCompleteSearch('/Assets/AssetsAPI/GetServiceRecord', id).then(
function(data) {
console.log(JSON.stringify(data));
});
};
}]);
})();
<div ng-controller="ServicesMasterCtrl">
<md-autocomplete md-items="item in FormService.AutoSearch.Search(id)" md-selected-item="FormService.AutoSearch.selectedItem" md-search-text-change="FormService.AutoSearch.SearchChanged(id)" md-search-text="id" md-min-length="2" md-delay="500" placeholder="Search service by id or name"
md-item-text="item.id" md-no-cache="true">
<md-item-template>
<span class="item-title">Id:{{item.id}} </span><span class="item-metadata">
<span class="item-metastat">Service: <strong> {{item.servicename}} </strong></span>
<md-icon md-svg-icon="octicon-repo.svg"></md-icon><span class="item-metastat" ng-if="!!item.montlyrental">MonthlyRental: <strong> {{item.montlyrental}} </strong></span><span class="item-metastat" ng-if="!!item.activationdate">ActiveDate: <strong> {{item.activationdate | ConvertToDate | date: 'dd-MMM-yyyy'}} </strong></span></span>
</md-item-template>
<md-not-found>No matching items found...</md-not-found>
</md-autocomplete>
</div>
有人可以帮我。
答案 0 :(得分:1)
我会简化LoadFormActivity
服务。底层不需要额外的承诺。
function NGAutoCompleteSearch(url, val) {
return $http.get(url, { params: {searchvalue: val} });
}