在Angular 4

时间:2017-06-10 22:08:55

标签: angular typescript

我正在尝试将所选行的值设置为组合框,但只加载输入

这是我的页面

`    <form class="form-horizontal form-label-left parsleyjs" method="post" data-parsley-priority-enabled="false" novalidate="novalidate">
      <fieldset>   
        <div class="form-group row">
          <label class="form-control-label labelName" for="basic-change">
           Name
          </label>
          <div class="inputName">
            <input #newRole type="text" id="basic-change" name="basic-change" class="form-control" data-parsley-minlength="4" data-parsley-trigger="change"
              maxlength="50" value="{{role.name}}" required="required">
          </div>
        </div> 
         <div class="inputName">
            <select #newEnv class="form-control" required="required">
                    <option value="b">Back Office</option>
                    <option value="c">Casino</option>
                    <option value="s">Store</option>
                </select>
          </div>
          div class="form-group row">
          <label class="form-control-label labelName" for="basic-change">
           Description
          </label>
          <div class="inputName">
            <input #newDesc type="text" id="basic-change" name="basic-change" class="form-control" data-parsley-minlength="4" data-parsley-trigger="change"
              value="{{role.description}}" required="required">
          </div>
          </fieldset>
         </form>

这是我的组件

export class RolesReplaceComponent implements OnInit {
@ViewChild('modal') public modal: ModalDirective;
mensaje: string;
error: boolean;
role;
private isLoading = false;
constructor(
private route: ActivatedRoute,
private router: Router,
private svrRole: RoleService,
private http: Http,
private trans: TranslateService, ) {
this.error = false;
 }

ngOnInit(): void {
jQuery('.parsleyjs').parsley();
this.isLoading = true;
this.svrRole = new RoleService(this.http);
this.route.params
  .subscribe(
  param => {
    this.svrRole.getById(param['id'], null)
      .subscribe(
      resp => {
        console.log(resp);
        this.role = resp.data.data;
        console.log(this.role);
        this.isLoading = false;
        console.log('Que sucedio?');
      },
      error => console.log("Error on edit-roles-services ", error));
    }
   );
 }
}

当我使用与用于加载输入相同的方式时,对<select>

不起作用

有些想法怎么做?

1 个答案:

答案 0 :(得分:0)

好吧,它只需要添加2行,但最终可行, 在组件上我必须添加这个变量:

options:any = [{value:'b', name:"Back Office"},{value:'c', name:"Casino"},{value:'s', name:"Store"}];

并将其替换为选项标签:

<option *ngFor="let p of options" [value]="p.value" [selected]="p.value == role.environment">{{p.name}}</option>