【问题标题】:Material angular autocomplete keeps giving me error材料角度自动完成不断给我错误
【发布时间】:2018-06-21 02:41:17
【问题描述】:

我正在尝试使demo 工作,但不知何故它不适合我。它一直给我错误

servers.component.ts

import { Component } from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import {startWith} from 'rxjs/operators/startWith';
import {map} from 'rxjs/operators/map';
@Component({
  selector: 'app-servers',
  templateUrl: './servers.component.html',
  styleUrls: ['./servers.component.css']
})

export class User {
  constructor(public name: string) { }
}

export class ServersComponent {

  myControl = new FormControl();

  options = [
    new User('Mary'),
    new User('Shelley'),
    new User('Igor')
  ];

  filteredOptions: Observable<User[]>;

  ngOnInit() {
    this.filteredOptions = this.myControl.valueChanges
      .pipe(
        startWith<string | User>(''),
        map(value => typeof value === 'string' ? value : value.name),
        map(name => name ? this.filter(name) : this.options.slice())
      );
  }

  filter(name: string): User[] {
    return this.options.filter(option =>
      option.name.toLowerCase().indexOf(name.toLowerCase()) === 0);
  }

  displayFn(user?: User): string | undefined {
    return user ? user.name : undefined;
  }
}

我在app.module.ts 中导入了User 类和ServersComponent

    import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AlertModule } from 'ngx-bootstrap';
import "hammerjs";

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {MatButtonModule, MatInputModule } from '@angular/material';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 
import { AppComponent } from './app.component';
import { ServerComponent } from './server/server.component';
import { ServersComponent, User } from './servers/servers.component';
import { MyFormComponent } from './my-form/my-form.component';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {MatAutocompleteModule} from '@angular/material/autocomplete';





@NgModule({
  declarations: [
    AppComponent,
    ServerComponent,
    ServersComponent,
    MyFormComponent,
    User,

  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AlertModule.forRoot(),
    FormsModule,
    ReactiveFormsModule,
    MatButtonModule,
    MatInputModule,
    MatCheckboxModule,
    MatAutocompleteModule,

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

但是,如果我使用这个 demo 它工作正常。

你能告诉我我在代码中做错了什么吗?

【问题讨论】:

    标签: angularjs autocomplete angular-material


    【解决方案1】:

    问题出在这一行,

    import { ServersComponent, User } from './servers/servers.component';
    

    通常您只能从一个选择/组件中拥有一个组件。从中删除用户。

    要在此问题上添加更多内容,您不应从同一组件导出两个类。将您的组件更改为,

    import { Component } from '@angular/core';
    import {FormControl} from '@angular/forms';
    import {Observable} from 'rxjs/Observable';
    import {startWith} from 'rxjs/operators/startWith';
    import {map} from 'rxjs/operators/map';
    @Component({
      selector: 'app-servers',
      templateUrl: './servers.component.html',
      styleUrls: ['./servers.component.css']
    })
    export class ServersComponent {
    
      myControl = new FormControl();
    
      options = [
        new User('Mary'),
        new User('Shelley'),
        new User('Igor')
      ];
    
      filteredOptions: Observable<User[]>;
    
      ngOnInit() {
        this.filteredOptions = this.myControl.valueChanges
          .pipe(
            startWith<string | User>(''),
            map(value => typeof value === 'string' ? value : value.name),
            map(name => name ? this.filter(name) : this.options.slice())
          );
      }
    
      filter(name: string): User[] {
        return this.options.filter(option =>
          option.name.toLowerCase().indexOf(name.toLowerCase()) === 0);
      }
    
      displayFn(user?: User): string | undefined {
        return user ? user.name : undefined;
      }
    }
    

    【讨论】:

    • 它给出了与屏幕截图相同的错误。它不会改变任何东西。以前我没有用户,我想我错过了添加用户,所以我添加了它,但错误存在。
    • @Santosh 我现在在答案中添加了更多详细信息
    • `new User('Mary'), `这个对象没有定义对吗?
    • 您可以在 .ts 文件中单独定义并导入
    • 谢谢。你太棒了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2021-02-23
    相关资源
    最近更新 更多