ng-selected不在选择框中使用ng-model - Angularjs

时间:2016-09-23 05:33:51

标签: angularjs angularjs-ng-repeat angularjs-ng-model angularjs-select

虽然ng-model用于选择框,ng-selected也用于选项,但某些条件是ng-selected时间不起作用。

如果我将删除ng-model而不是ng-selected工作,但是如果我将删除ng-model而不是我应该如何获得控制器中选择框的值。

请帮忙!

这是我的代码......

HTML:

<select class="form-control" ng-change="accessModeSelected()">
     <option ng-selected="mode.status == '1'"  ng-repeat="mode in storageResult.accessMode" ng-value="mode.name" name="accessMode{{$index}}" id="accessMode">
           {{mode.name}}
     </option>
</select>

AngularJS:

$scope.storageResult   = {
           "storageAccount":"Enable", 
           "user": "sdcard",
           "pass": "sdcard",
           "wifiIP": "0",
           "ipAddr": "0",
           "accessMode": [
                {"name": "Local Storage","status": "0"},
                {"name":"WiFi Storage", "status": "1"},
                {"name":"Internet Storage", "status": "0"}
            ]
         }

3 个答案:

答案 0 :(得分:8)

使用ng-optionsng-init(用于设置默认值)而不是ng-repeat

ng-options专门针对select

<select class="form-control" ng-init="statusselect='WiFi Storage'" ng-model="statusselect" ng-options="mode.name as mode.name for mode in storageResult.accessMode">

</select> Selected Value : {{statusselect}}

<强> FIDDLE

修改:使用ng-repeat

我更希望ng-options,但如果您想ng-selectedng-repeat一起使用,则需要从ng-modelcontroller提供默认选定值1}}

<select class="form-control" ng-model="statusselect">
     <option ng-selected="{{mode.name == statusselect}}" ng-repeat="mode in storageResult.accessMode" value="{{mode.name}}" name="accessMode{{$index}}" id="accessMode">
           {{mode.name}}
     </option>
</select> Selected Value : {{statusselect}}

内部控制器

$scope.storageResult  = {
           "storageAccount":"Enable", 
           "user": "sdcard",
           "pass": "sdcard",
           "wifiIP": "0",
           "ipAddr": "0",
           "accessMode": [
                {"name": "Local Storage","status": "0"},
                {"name":"WiFi Storage", "status": "1"},
                {"name":"Internet Storage", "status": "0"}
            ]
         }
   $scope.statusselect = $scope.storageResult["accessMode"][1]["name"];

<强> FIDDLE

答案 1 :(得分:2)

使用ng-options代替ng-repeat

<select ng-model="status"  ng-options="mode.status as mode.name for mode in storageResult.accessMode">

和控制器

app.controller("dobController", ["$scope", "$http",
  function($scope, $http) {

    $scope.storageResult = {
      "storageAccount": "Enable",
      "user": "sdcard",
      "pass": "sdcard",
      "wifiIP": "0",
      "ipAddr": "0",
      "accessMode": [{
        "name": "Local Storage",
        "status": "0"
      }, {
        "name": "WiFi Storage",
        "status": "1"
      }, {
        "name": "Internet Storage",
        "status": "0"
      }]
    };
    $scope.status = '1';

  }
]);

<强> DEMO

答案 2 :(得分:1)

我确定有很多方法可以回答这个问题并提供了很多答案,但是基于OP关于如何发送&#39; mode.name&#的问题39;进入ng-model我在下面使用ng-options进行了解释。

在你的JS中

var app = angular.module('todoApp', []);

    app.controller("dobController", ["$scope", "$http",
        function($scope, $http) {


            $scope.storageResult = {
                "storageAccount": "Enable",
                "user": "sdcard",
                "pass": "sdcard",
                "wifiIP": "0",
                "ipAddr": "0",
                "accessMode": [{
                    "name": "Local Storage",
                    "status": "0"
                }, {
                    "name": "WiFi Storage",
                    "status": "1"
                }, {
                    "name": "Internet Storage",
                    "status": "0"
                }]
            };
            $scope.status = $scope.storageResult.accessMode[0].name;
            $scope.selectedItem = '';

        }
    ]);

在HTML中将模式名称绑定到ng-model,请遵循以下步骤。

<select ng-model="status"  ng-options="mode.name as mode.name for mode in storageResult.accessMode">

要绑定整个对象,您可以尝试以下语法

<select ng-model="status"  ng-options="mode as mode.name for mode in storageResult.accessMode">

为此,在JS $scope.status中进行小的更改,如

$scope.status = $scope.storageResult.accessMode[0];

这是一个DEMO plunker