TypeError:无法读取角度4中未定义的属性“length”

时间:2018-04-09 16:40:12

标签: angular primeng

TypeError:无法读取角度4中未定义的属性“长度”

这是代码     导出类UserComponent实现OnInit {

  roles:IUserRole[];
  sourseRoles: SelectedItem[];
  selectedRole:any;

BindRoles() {
    this.sourseRoles= [];
    this.sourseRoles.push({ label: "Assign Role", value: null });
    if (this.roles.length > 0) {
      for (let role of this.roles) {
        this.sourseRoles.push({ label: role.Title, value: role.UserRoleId })enter image description here
      }
    }
  }
----------

这是代码

  

this.appServiceManager.libGet( “角色”)                .subscribe((role:any)=> {                  this.selectedRole = [];

           this.selectedRole = role;
           this.sourseRoles.push({ label: this.selectedRole.Title, value: this.selectedRole.UserRoleId })
         },
         (error: any) => {
           this.msgs = [];

           this.msgs.push({ severity: 'error', summary: 'Error Message', detail: error });
         });
----------
<label for="password">User Role</label>
 <p-dropdown name="Title" #Title="ngModel" required (click)="BindRoles()" 

 [(ngModel)]="selectedRole" id="dropdown" [options]="sourseRoles"

 [autoWidth]="false"></p-dropdown>

 <div *ngIf="registerForm.submitted && !Title.valid" class="help-block">Role 
 Information is required</div>

----------

2 个答案:

答案 0 :(得分:1)

export class UserComponent implements OnInit {
    roles: IUserRole[] = [];     <======= Initialize it =========>
    sourseRoles: SelectedItem[]; <======= Declaration was incorrect =========>
    selectedRole: any;

    BindRoles() {
      if (this.roles.length > 0) {
        for (let role of this.roles) {
          this.sourseRoles.push({ label: role.Title, value: role.UserRoleId })
        }
      }
    }
}

答案 1 :(得分:0)

我在TS代码中没有看到这一点:

sourseRoles

我看到你的角色和selectedRole