gpt4 book ai didi

Angular 2 ng-select 多个选定值问题

转载 作者:太空狗 更新时间:2023-10-29 18:26:19 25 4
gpt4 key购买 nike

我正在研究 Angular 2 并使用库 Angular 2 选择组件 enter link description here

我需要设置默认项目选择但是当我显示选择值时我得到了一些错误。我也检查了这个链接 enter link description here寻求帮助,但对我不起作用

错误是:

EXCEPTION: Uncaught (in promise): Error: Error in ./Jobs class Jobs - inline template:40:6 caused by: Cannot set property 'selected' of undefined

模板:

<form class="parsleyjs" data-validate="parsley" data-parsley-validate [formGroup]="form">
<div class="row">
<div class="col-md-12">
<section widget class="widget">
<header>
<h4>
<span class="fw-semi-bold">Jobs</span>
</h4>
</header>
<div class="widget-body">
<ng-select
[options]="jobOptions"
multiple="true"
placeholder="Select Multiple Jobs"
formControlName="selectedJobsMultiple"
(selected)="jobsSelected($event)"
(deselected)="jobsDeselected($event)">
</ng-select>
</div>
</section>
</div>
</div>
</form>

模块:

import 'parsleyjs';

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { Jobs } from "./jobs.component";
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {SelectModule} from 'angular2-select';
export const routes = [
{path: '', component: Jobs}
];

@NgModule({
imports: [CommonModule, RouterModule.forChild(routes), FormsModule, SelectModule, ReactiveFormsModule],
declarations: [Jobs],
})
export default class JobsModule {
static routes = routes;
}

组件:

import { Component,ViewEncapsulation, Input ,Renderer, ElementRef, OnDestroy, OnInit } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {Observable} from "rxjs/Rx";
import {FormControl, FormGroup} from '@angular/forms';

declare var jQuery, $:any;

@Component({
selector: 'jobs',
templateUrl: './jobs.template.html',
encapsulation: ViewEncapsulation.None,
})
export class Jobs implements OnInit {

jobSiteOptions: Array<any> = [];

form: FormGroup;

constructor() {
this.jobOptions= [
{
value: 'a',
label: 'Alpha'
},
{
value: 'b',
label: 'Beta'
},
{
value: 'c',
label: 'Gamma'
}
];
}


jobsSelected(item) {
console.log('- jobs selected (value: ' + item.value + ', label:' +
item.label + ')');
}

jobsDeselected(item) {
console.log('- jobs deselected (value: ' + item.value + ', label:' +
item.label + ')');
}

ngOnInit():void {
jQuery('.parsleyjs').parsley();

this.form = new FormGroup({});
this.form.addControl('selectedJobsMultiple', new FormControl(['a','b']));
}

}

最佳答案

看起来你通过了 jobOptions 而不是 jobSiteOptions

<ng-select 
[options]="jobSiteOptions"
multiple="true"
placeholder="Select Multiple Jobs"
formControlName="selectedJobsMultiple"
(selected)="jobsSelected($event)"
(deselected)="jobsDeselected($event)">
</ng-select>

关于Angular 2 ng-select 多个选定值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40582582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com