Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 当我从api获取数据并在initData中强制转换数据时,ng2选择NotWorking 细节_Angular_Typescript - Fatal编程技术网

Angular 当我从api获取数据并在initData中强制转换数据时,ng2选择NotWorking 细节

Angular 当我从api获取数据并在initData中强制转换数据时,ng2选择NotWorking 细节,angular,typescript,Angular,Typescript,我在表单中使用ng2 select。我从api中检索数据并将其设置到数组中,但它无法正常工作。下面是我的密码。当我硬编码时,它工作得很好。我不知道为什么会这样做,可能是因为它在响应获取数据后使用空数据初始化数组,但没有将其设置为数组 import { Component, OnInit } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import {Observable} from

我在表单中使用ng2 select。我从api中检索数据并将其设置到数组中,但它无法正常工作。下面是我的密码。当我硬编码时,它工作得很好。我不知道为什么会这样做,可能是因为它在响应获取数据后使用空数据初始化数组,但没有将其设置为数组

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import {Observable} from 'rxjs/Observable';
import { AuthService } from '../../services/auth/auth.service';
import {SELECT_DIRECTIVES} from 'ng2-select';

@Component({
selector: 'users-edit',
templateUrl: '../../app/components/user/user-edit.html',
directives: [SELECT_DIRECTIVES]
})
export class UserEditComponent implements OnInit{
private isAdmin: Boolean = false;
private _data: Observable;
private fname: string;
private id: number;
private lname: string;
private email: string;
private _roles: Observable;
public roles: any = [];
public groups: any = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5'];
private initRoleData: Array[]=[]; 
private _disabledV: string = '0';
private disabled: boolean = false;


constructor(private router: Router,
    private userService: UserService,
    private route: ActivatedRoute,
    private authService: AuthService) {

    this.route.params.subscribe(params => {
        this.id = +params['id'];
    });
}

ngOnInit() {
    this.userService.getUser(this.id)
        .subscribe(data => {
            this.fname = data.FirstName;
            this.lname = data.LastName;
            this.email = data.Email;
        });
    this.userService.getUserRolesById(this.id)
        .subscribe(data => {
            data.forEach(role => {
                this.initRoleData.push(role.Name);             
            });
        });
    console.log(this.initRoleData);
    this.userService.getAllRoles()
        .subscribe(data => {
            data.forEach(role => {
                this.roles.push(role.Name);
            });
        });
    console.log(this.roles);
}
}
html

编辑用户帐户
输入名字
输入姓氏
输入电子邮件地址
角色
取消
拯救
关于这个问题有很多问题

jkwiz
找到的解决方法是将您的选择包装在一个
*ngIf
div中,使您的选择延迟,因为只有在查询获得您的数据后才会创建选择:

    ...
    ...
           <section>
                <label>
                    Roles
                </label>
                <div *ngIf="roles.length > 0">
                    <ng-select 
                               [multiple]="true"
                               [items]="roles"
                               [disabled]="disabled"
                               (data)="refreshValue($event)"
                               (selected)="selected($event)"
                               (removed)="removed($event)"
                               placeholder="No roles assign">
                    </ng-select>
                </div>
            </section>
    ...
    ...
。。。
...
角色
...
...
问题是您无法将行添加到select dynamicali,而且这似乎是
ng2 select
上的一个问题,我想它不会立即修复,因为这个问题有点老了,即使它是一个关键功能

关于这个问题没有什么可担心的

jkwiz
找到的解决方法是将您的选择包装在一个
*ngIf
div中,使您的选择延迟,因为只有在查询获得您的数据后才会创建选择:

    ...
    ...
           <section>
                <label>
                    Roles
                </label>
                <div *ngIf="roles.length > 0">
                    <ng-select 
                               [multiple]="true"
                               [items]="roles"
                               [disabled]="disabled"
                               (data)="refreshValue($event)"
                               (selected)="selected($event)"
                               (removed)="removed($event)"
                               placeholder="No roles assign">
                    </ng-select>
                </div>
            </section>
    ...
    ...
。。。
...
角色
...
...
问题是您无法将行添加到select dynamicali,而且这似乎是
ng2 select
上的一个问题,我想它不会立即修复,因为这个问题有点老了,即使它是一个关键功能