0

我在使用 ng-options 填充选择的同一函数中从数组中选择一个值时遇到了一些困难。我正在尝试动态加载一个充满选项的选择列表,同时在用户上次选择我想要选择的内容时有一个默认值。当 console.logging 我可以看到这有效并被添加到 machineProfile.input 但选择不反映 ng-model 作为当前值的内容。我确实确认它们都是一样的。我在下面有一些代码。

 $scope.getMachineProfile = function(object) {

    var count = 0;
    var keepGoing = true;

    $scope.machineProfile.machineName = object.Name;

    $scope.machineInputs = object.Hardware;
    /*angular.forEach(object.Hardware, function(inputs, key) {

      console.log($scope.machineInputs);
    });*/

    //var foundMachine = 0;
    angular.forEach($scope.machineProfiles, function(machine, key) {
      if (keepGoing) {
        if (machine.remoteAddr === object.Address) {
          keepGoing = false;


          /*$timeout(function(){

          }, 500);*/
          /*console.log($scope.machineProfiles[count].input);
          console.log($scope.machineProfile.input);*/
          $scope.machineProfile.input = $scope.machineProfiles[count].input;
          $scope.machineProfile.workType = $scope.machineProfiles[count].workType;
          $scope.machineProfile.workPeriod = $scope.machineProfiles[count].workPeriod;
          $scope.machineProfile.counterRate = $scope.machineProfiles[count].counterRate;
          $scope.machineProfile.timerRate = $scope.machineProfiles[count].timerRate;
          console.log($scope.machineProfile);
          //console.log('Awesome select: ' + count);
          //console.log($scope.machineProfiles[count].input);
        }
        ++count;
      }
    });

HTML

<ul class="nav sidebar-menu">
        <li ng-repeat="machine in machineObj" class="">
            <a ng-click="getMachineProfile(machine)">
                <span class="fa fa-cogs"></span>
                <span class="sidebar-title">{{machine.Name}}</span>
            </a>
        </li>
    </ul>

下面是从上面的代码中选择一个项目后:

<div class="col-sm-4">
        <div class="form-group">
            <label for="chooseInput" class="control-label">Choose Input</label>
            <!-- <select name="chooseInput" class="form-control"  ng-model="machineProfile.input">
                <option ng-selected="machine.input === machineProfile.input" ng-repeat="machine in machineInputs" value="machine.input">
                    {{machine.input === machineProfile.input}}
                </option>
            </select> -->
            <select name="chooseInput" class="form-control" ng-model="input" ng-change="awesome(machineProfile.input)" ng-options="machine.input for machine in machineInputs track by machine.input"></select>
            <!-- <input disabled="true" name="machineName" type="text" class="form-control" placeholder="Machine Name"> -->
        </div>
    </div>

感谢任何帮助,因为这让我发疯。$scope 对象 machineProfile.input 已更新,但屏幕上没有出现任何内容。

4

1 回答 1

0

看起来你ng-model的不正确。尝试这个。

<select name="chooseInput" class="form-control" ng-model="machineProfile.input" ng-change="awesome(machineProfile.input)" ng-options="machine.input for machine in machineInputs track by machine.input"></select>
于 2015-05-29T20:05:45.710 回答