2017-05-26 224 views
1

我將一個項目從角到Web組件/自定義元素,我試圖通過創建下列文本域綁定,以取代ng-model:因爲這訪問屬性

<input type="search" class="form-control search_input" placeholder="Search for someone new" value$="[[userLookup:input]]" required autocomplete="off"> 

顯然從角轉換,我需要能夠在一個JavaScript函數來訪問該值:從lookupUser FUNC內

(function(customElements) { 
    class RecentSearch extends PolymerMixins.LightDomMixin(Polymer.Element) { 
     static get is() { return 'recent-search'; } 

     static get properties() { 
      return { 
       properties: { 
        user: { 
         type: Object 
        }, 
        userLookup: { 
         type: String, 
         reflectToAttribute: true, 
         value: '', 
        }, 
       }, 
      }; 
     } 

     lookupUser() { 
      if (this.userlookup) { 
       $state.go('users', { query: userlookup }); 
      } 
     }; 
    } 
    customElements.define(RecentSearch.is, RecentSearch); 
})(window.customElements); 

我將如何訪問userLookup屬性(在一個綁定到文本字段)重刑?

回答

1

您已經與this.userLookup訪問userLookup正確lookupUser()。事件處理程序的上下文(即this)是Polymer元素實例。

但是,您的數據綁定不正確地是單向數據綁定,因此userLookup不會被更新。這種binding需要是雙向的(即,用大括號),並且不能使用使用屬性綁定(即,$=)。

正確的用法應該是這樣的:

<input placeholder="Search for someone new" 
     value="{{userLookup::change}}" /> 

demo