2017-03-06 57 views
0

我在我的Angular.js網站上實現了一個md-autocomplete。搜索和選擇工作,它更新顯示的列表和一切。但由於某些原因,我無法閱讀所選項目。無論我如何定義我的md-selected-item,它總是會保持null。 selectItem(item)中的項目和範圍中的值都不會更改。md-autocomplete沒有返回所選項目

AngularCode:

controllers.controller('UniSearch',function(SearchUniversity){ 
    var self = this; 
    self.query = query; 
    self.selectItem = selectItem; 

    function query (searchText) { 
     unis = SearchUniversity.get({ query : self.queryText }); 
     return unis.$promise.then(function(data) { 
      var thing = []; 
      // Just some code to reformat the data coming from the server 
      angular.forEach(data,function(value,key){ 
       if (angular.isUndefined(value.UNI_ID)){ 
        return 
       } 
       thing.push(value); 
      }); 
      console.log(thing) 
      return thing; 
     }); 
    } 

    function selectItem(item) { 
     console.log(item); 
    } 
}); 

HTML:

<div ng-controller="UniSearch as ctrl"> 
    <md-autocomplete 
     md-selected-item="ctrl.selected" 
     md-search-text="ctrl.queryText" 
     md-items="uni in ctrl.query(queryText)" 
     md-item-text="ctrl.selected.uni_international_name" 
     md-autoselect="true" 
     placeholder="Search for you city" 
     md-selected-item-change="ctrl.selectItem(item)"> 
     <md-item-template> 
      <span md-highlight-text="queryText" md-highlight-flags="^i"> 
       {{uni.uni_international_name}} 
      </span> 
     </md-item-template> 
    <md-not-found> 
     No universities matching "{{ctrl.queryText}}" were found. 
    </md-not-found> 
</md-autocomplete> 

</div> 

我缺少什麼? THX

+0

有什麼辦法可以將它放在小提琴或codepen上,以方便幫助嗎? –

+0

我有問題讓小提琴工作。這是一個相對較大的代碼庫的一部分。只是想知道是否有什麼明顯的錯誤... – Jason

回答

1

是很難不運行的代碼進行調試,但似乎有點怪你enumarate的itens專門在md-selected-itemmd-itens通話onther功能調用uni通過item

+0

工作。在這種情況下,並不期待這個名字重要。 – Jason

+0

樂意提供幫助,所以請您將它標記爲正確答案? –

+0

我從來沒想通過Luiz的意思,但我的回答是,md-selected-text和md-item-template需要解析到同一個東西,所以我有效地使用了'md-item-text =「uni.uni_international_name 「'它爲我工作。 –

相關問題