为什么我得到“无法读取'未定义'的'价值'?

时间:2018-06-19 09:26:14

标签: javascript angular

我收到此错误消息,我不确定为什么会发生这种情况。这是代码:

<div class="container">
  <h2>New rule definition</h2>
  <hr>
  <form action="#">
    <select name="engines" #eng class="float-right" (change)="act">
      <option value="0">pick engine</option>
      <option *ngFor="let engine of engines" value='{{engine.engineId}}'>{{ engine.engineId }}</option>
    </select>

    <div class="form-group">
      <label for="description" class="col-form-label-lg">Description</label>
      <textarea name="description" (ngModel)="description" #description class="form-control" cols="30" rows="6"></textarea>
    </div>

    <div class="form-group">
      <label for="statement" class="col-form-label-lg">Statement</label>
      <textarea name="statement" (ngModel)="statement" #statement class="form-control" cols="30" rows="6"></textarea>
    </div>

    <div class="form-group" #act *ngIf="eng.value=='perseo-fe'">
      <label>Action</label>
      <br> Type:
      <input type="text" name="type" (ngModel)="actionType" #actionType />
      <br><br> Parameters:
      <br>
      <input type="text" name="name" placeholder="name" (ngModel)="actionName" #actionName />
      <br> Value:
      <select #actionValue (ngModel)="actionValue">
        <option value="true">true</option>
        <option value="false">false</option>
      </select>
      <br>
      <input type="text" name="type2" (ngModel)="typeValue" #typeValue placeholder="type" />
    </div>

    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="enable">
      <label for="enable" class="form-check-label">Enable</label>
    </div>
    <br>

    <button class="btn btn-primary"
      (click)="addRule(
        eng.value,
        description.value,
        statement.value,
        actionType.value,
        actionName.value,
        actionValue.value,
        typeValue.value
      )"
    >
      Add
    </button> &nbsp;
    <button class="btn btn-secondary" (click)="goBack()">Back</button>
  </form>

</div>

我希望#act div在用户从select中选择'perseo-fe'之前是不可见的。我设法设置它,但当我尝试发送我输入的数据时,我得到'未定义'。奇怪的是,当我删除*ngIf时,它有效吗?

1 个答案:

答案 0 :(得分:0)

尝试将&#34; eng?.value&#34;。

&#39;?&#39;如果您的&#39; eng&#39;之后的值不会触发错误。尚未定义。