html - TypeError:无法读取未定义的属性“加入”
问题描述
我正在尝试将 select2 集成到我的 html 模板中,但我不断收到此错误。我希望能够一次选择多个选项我不知道该怎么做。帮助将不胜感激。
管理员.ts
import { Component, OnInit } from '@angular/core';
import { Select2OptionData } from 'ng2-select2';
@Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss']
})
export class AdminComponent implements OnInit {
public candidateData: Array<Select2OptionData>;
public options: Select2Options;
public value: string[];
public current: string;
constructor() { }
ngOnInit() { this.candidateData = [
{ id: '1', text: 'Mohammadu Buhari' },
{ id: '2', text: 'Atiku Abubakr' },
{ id: '3', text: 'John Doe'},
{ id: '4', text: 'Someone Else'}];
this.options = {
multiple: true
};
this.current = this.value.join(' | ');
}
changed(data: {value: string[]}) {
this.current = data.value.join(' | ');
}
}
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2">
<label>Candidates</label>
<select2
[data] = "exampleData"
[options] = "options"
[width] = "500"
[value] = "value"
(valueChanged) = "changed($event)"
></select2>
</div>
</div>
</div>
解决方案
您必须初始化您的模型,因为undefined
当您尝试在ngOnInit
.
public value: string[] = [];
推荐阅读
- android - 如何解析对象的JSON响应数组,其中数组没有键名
- python - 重构 Django 布尔查询
- javascript - 自执行功能是否被认为是 nodejs 模块中的一个好习惯?
- php - 如何访问此 JSON 信息 (PHP)?
- python - Python没有像我预期的那样增加循环
- java - 为什么执行一组测试会给出不同的结果,如果单独执行?
- raspberry-pi3 - Raspberry pi 文本输入语音输出上的 Google Assistant
- python - 应用程序和服务列表?
- arrays - 使用结构类型时出现意外错误
- django - 如何从 django 框架中的 ModelForm 字段中获取特定值?