【问题标题】:ng-repeat with input text box track by $indexng-repeat 输入文本框跟踪 $index
【发布时间】:2016-09-14 14:51:22
【问题描述】:

我正在编写一个应用程序,其中一个 div 包含日期选择器、2 个时间选择器和一个输入文本框。我在 div 之外有一个复选框。每次当我选中一个复选框时,都会添加一个日期位置的 div。我的应用程序工作正常,除了我的输入框值。 ng-model 没有保存文本框的值

<div class="dateContainer"  ng-repeat="selecting in selects track by $index">
  <div class="datePicker">  
    <button bs-datepicker>
      --- some code
    </button>
  </div>
  <div class="timePicker">  
    <button bs-timePicker>
      --- some code
    </button>
  </div>
  <label class="configLocation" for="configLoc">Location</label>
  <input class = "form-control"type="text" ng-change ="DateArr($index)"  style="font-weight: normal;"name ="configLocation" class="configLocation"  ng-model = "selects[$index].configLoc" />
</div>

我保留了我的模型名称选择[$index].configLoc,因为我试图根据每个索引值进行保存。

我的控制器- 我正在初始化 $scope.selects,如下所示。

$scope.selects = [{commDate : null,commEtime: null,commStime: null,configLoc: ''}];

当我在更改事件上调用 DateArr($index) 时,我无法获取输入的值 $scope.selects[0].configLoc ??值总是显示''。为什么??

【问题讨论】:

标签: javascript angularjs


【解决方案1】:

你必须在 ng-repeat 或 ng-model 中使用 $index ... 只是改变:

ng-repeat="selecting in selects"
  (or)
ng-model="selecting.configLoc"

【讨论】:

    【解决方案2】:

    对于每个 ng-repeat,都会使用重复项创建一个新范围,即选择。

    ng-model = "selects[$index].configLoc"
    

    变成

    ng-model = "selecting.configLoc"
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      相关资源
      最近更新 更多