表单对象AngularJs中不存在表单字段

时间:2018-12-05 12:23:14

标签: angularjs angularjs-directive angularjs-ng-model

我的表单具有几个字段,但是所有字段都存在于表单对象中,并且不存在具有名称扇区的字段。为什么?以及我该如何解决?

public class BatchFile {

    public void BatchFile(File file) throws IOException, InterruptedException 

        ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
        processBuilder.directory(file.getParentFile());
        Process process = processBuilder.start();
        Scanner scanner = new Scanner(process.getInputStream());
        while(scanner.hasNextLine()){
            System.out.println(scanner.nextLine());
        }
        scanner.close();
        System.out.println(process.waitFor());
    }

因此,对象中存在现场公司,但部门中没有

我不使用ng-model,因为在指令内部设置了扇区:

<form name="candidateForm" ng-submit="submitForm()">
    <div class="item item-top">
      <label>{{'Company'|translate}}*</label>
      <input company-autocompleter class="companyAutocompleterOuterSelect"
             ng-maxlength="100" name="company" ng-model="candidate.company"
             type="text" ng-change="progressUpdate()" required>
      <div class="alert alert-danger"
           ng-show="candidateForm.company.$invalid && !candidateForm.company.$pristine && candidateForm.company.$error.required == true">
          {{'Enter a company'|translate}}
      </div>
   </div>

   <div class="item industry">
      <label>{{'Sector'|translate}}*</label>
      <input sector-autocomplete name="sector" type="text"
             class="select2-container form-control input-lg select2 select14 widthSelectInput1"
             required>
       <div class="alert alert-danger"
            ng-show="candidateForm.sector.$invalid && !candidateForm.sector.$pristine && candidateForm.sector.$error.required">
            {{'Enter a sector'|translate}}
       </div>
  </div>
</form>

3 个答案:

答案 0 :(得分:2)

sector-autocomplete指令需要与ngModelController一起使用:

app.directive("sectorAutocomplete", function() {
    return {
        require: "ngModel",
        link: function(scope, elem, attrs, ngModel) {
            elem.select2({
                minimumInputLength: 0,
                placeholder: $translate.instant('Sector'),
                allowClear: true,
                data: translatedSectors,
                dropdownCssClass: "bigdrop"
            }).unbind("change").on("change", function(e) {                
                if (e.added) {
                    ngModel.$setViewValue(e.added.id);
                } else {
                    ngModel.$setViewValue("");
                }
            })
        }
    }
})

用法:

<input sector-autocomplete name="sector" type="autocomplete"
       ng-model="candidate.sector" ng-change="progressUpdate()"
       class="select2-container form-control input-lg select2 select14 widthSelectInput1"
       required />

需要ngModelController才能向ngFormController注册控件。

有关更多信息,请参见

答案 1 :(得分:0)

您需要使用ng-model绑定输入数据

<input sector-autocomplete name="sector" type="text"
       ng-model="candidate.sector"
       class="select2-container form-control input-lg select2 select14 widthSelectInput1"
       required>

答案 2 :(得分:0)

candidateForm是您的验证对象,candidate.sector需要添加到ng-model