类型“ HomeComponent”上不存在属性“环境”

时间:2019-05-07 21:31:07

标签: angular6

我正在尝试使用[(ngModel)]将环境的值绑定到选择下拉列表。我收到错误消息“ HomeComponent上不存在属性环境。”

home.component.html

<select class="form-control" [(ngModel)]="envModel.environment"
[name]="environment" (change)="onEnvSelect(envModel.environment)">
<option [value]="env.id" *ngFor="let env of constLists.Environment"> 
{{env.value}}
</option>
</select>

home.component.ts

import { EnvironmentModel } from './home-landing.model';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})

export class HomeComponent implements OnInit {
  envModel = {} as EnvironmentModel;
}

onEnvSelect(env) {
    this.envModel.environment = env;
}

home.model.ts

export interface EnvironmentModel {
    environment: string;
}

错误发生:

app \ home.landing.component.html中的错误:“ HomeComponent上不存在属性环境。”

1 个答案:

答案 0 :(得分:0)

首先,您必须将所有代码放在Home.Component.ts中的HomeComponent类中

import { EnvironmentModel } from './home-landing.model';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})

export class HomeComponent implements OnInit {
  envModel = {} as EnvironmentModel;


onEnvSelect(env) {
    this.envModel.environment = env;
}
}

是本机着陆还是home.model?