2017-08-06 21 views
-1

我一直試圖在anuglarjs工廠中進行可重複使用的自動搜索。 這是我的代碼。但不幸的是得到錯誤:「不能讀取屬性」,然後「未定義」。試圖搜索谷歌和這裏在堆棧溢出,但未能解決它。

(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>

是否有人可以幫助我在這裏。

回答

1

我會簡化LoadFormActivity服務。底部不需要額外的承諾。

function NGAutoCompleteSearch(url, val) { 

    return $http.get(url, { params: {searchvalue: val} }); 

} 
+0

** **詹姆斯,我仍然得到錯誤:類型錯誤:無法讀取屬性 '然後' 的定義。我不知道它是由角度材質自動完成還是我的$ http請求引起的錯誤。如果認識任何人,請在這裏告訴我。謝謝 – Scavenger

+0

我建議在控制器中註銷'loadFormActivity.'和'loadFormActivity.NGAutoCompleteSearch'的值 – user2954463

相關問題