使NG-Options值选择与填充列表相同的功能

时间:2015-05-29 20:00:53

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

我在使用ng-options填充select的相同函数中从数组中选择值时遇到一些困难。我正在尝试动态加载一个包含选项的选择列表,同时从上次用户选择我想要选择的内容时有一个默认值。当console.logging我可以看到它工作并被添加到machineProfile.input但是select没有反映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已更新,但屏幕上没有任何内容。

1 个答案:

答案 0 :(得分: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>