简体   繁体   English

Angular Material md-autocomplete不适用于从Angular1演示中恢复的Angular2代码

[英]Angular Material md-autocomplete does not work with Angular2 code restated from Angular1 demo

I am using ng2-material to enable Material Design in an Angular2 app. 我正在使用ng2-material在Angular2应用中启用Material Design。 It seems to be a wrapper over the Angular1 MD code. 它似乎是Angular1 MD代码的包装。 Lots works, but I can't get md-autocomplete to work. 很多作品,但我不能使md-autocomplete起作用。 The problems are: 1. the Home component does not load. 问题是:1. Home组件无法加载。 2. get a Console error - loadAll() is not defined, 3. no form input box appears when I remove the JS to get something to load, 4. get a typescript error in Atom editor that each function statement expects ' ; 2.出现控制台错误-未定义loadAll(),3.删除JS进行加载时,没有表单输入框出现,4.在Atom编辑器中出现打字稿错误,每个函数语句均期望'; ' before the { ' 之前 {

Sorry for so much code, but I'm hoping the problem(s) is simple. 很抱歉有这么多代码,但我希望问题很简单。
The HTML copied from the MD example is: 从MD示例复制的HTML为:

 <md-list-item layout="column" class= "md-padding">
    <form (click)="$event.preventDefault()"> 
      <md-autocomplete
        md-selected-item="selectedItem"
        md-search-text-change="searchTextChange(searchText)"
        md-search-text="searchText"
        md-selected-item-change="selectedItemChange(item)"
        md-items="item in querySearch(searchText)"
        md-item-text="item.display"
        md-min-length="0"
        <md-item-template>
          <span md-highlight-text="searchText" md-highlight-flags="^i">{{item.display}}</span>
        </md-item-template>
        <md-not-found>
          No "{{searchText}}" language was found.
        </md-not-found>
        STATE
      </md-autocomplete>
    </form>
  </md-list-item>

The example at material.angularjs.org/latest/demo also has Angular1 javascript. material.angularjs.org/latest/demo上的示例还具有Angular1 javascript。 I tried to convert it to Angular2 by: 1. removing ctrl, 2.put the "this" statements in constructor(), 3.changed the function statements to object notation, but kept the logic, 4.changed $log to console.log, 5. removed $q in an unused query and replaced with console.log. 我尝试通过以下方式将其转换为Angular2:1.删除ctrl,2.将“ this”语句放入Constructor()中,3.将功能语句更改为对象表示法,但保留逻辑,4.将$ log更改为控制台。日志,5.在未使用的查询中删除了$ q并替换为console.log。 So my Angular2 code is: 所以我的Angular2代码是:

export class Home {
constructor () {
var self = this;
self.simulateQuery = false;
self.isDisabled    = false;
// list of `state` value/display objects
self.states = loadAll();
self.querySearch = querySearch;
self.selectedItemChange = selectedItemChange;
self.searchTextChange = searchTextChange;
self.newState = newState;
function newState(state) {
alert("Sorry! You'll need " + state + " first!");
}
querySearch (query, string) {
var results = query ? self.states.filter( createFilterFor(query) ) : self.states,
    deferred;
if (self.simulateQuery) {
  console.log('self.simulateQuery is now true ' + text);;
} else {
  return results;
}
}
searchTextChange(text, string) {
console.log('Text changed to ' + text);
}
selectedItemChange(item, string) {
console.log('Item changed to ' + JSON.stringify(item));
}
loadAll(state, string) {
var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware';
return allStates.split(/, +/g).map( function (state) {
  return {
    value: state.toLowerCase(),
    display: state
  };
});
}

createFilterFor(query, string) {
var lowercaseQuery = angular.lowercase(query);
return function filterFn(state) {
  return (state.value.indexOf(lowercaseQuery) === 0);
};
}

} //end of constructor()
} //end of Home

At the ng2-materials Example page for various types of input, https://justindujardin.github.io/ng2-material/#/components/input , I finally discovered that the <> symbol opens the working examples to expose the code. 在用于各种输入类型的ng2-materials示例页面上, https: //justindujardin.github.io/ng2-material/#/components/input,我终于发现<>符号打开了工作示例以暴露代码。 It is significantly different than my above attempt to transcribe. 这与我上面的转录尝试有很大不同。 But that example code works. 但是该示例代码有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM