无法在angularJs中设置DropDown的选定值

Ami*_*mar 13 javascript jquery angularjs

我有一个DropDown.我正在使用ng-repeatOption 绑定它的值.我想仅使用值字段设置所选值.

这是我的代码.

<div ng-controller="myCtrl">
    <select ng-model="selectedItemvalue">
        <option value="">--Select --</option>
        <option ng-repeat="sel in selectables" value="{{sel.value}}">{{sel.label}}</option>
    </select>
    <p>Selected Value is : {{selectedItemvalue}}</p>
</div>
Run Code Online (Sandbox Code Playgroud)

JS

angular.module('myApp', [])

// controller here
.controller('myCtrl', function($scope) {
    $scope.selectables = [
        { label: 'A', value: 1},
        { label:'B', value: 2},
        { label: 'C', value: 3}
    ];

    // this is the model that's used for the data binding in the select directive
    // the default selected item
    //using value, i want to set the selected value
    $scope.selectedItemvalue = "2"; 
})
Run Code Online (Sandbox Code Playgroud)

我的小提琴

如您所见,我想仅设置选定值uisng通过设置值.

A.B*_*A.B 21

您需要使用ng-model而不是将ng-value其绑定到模型.

<select ng-model="selectedItemvalue">
Run Code Online (Sandbox Code Playgroud)

更新: $scope.selectedItem需要$scope.selectedItemvalue在控制器中,然后您可以使用ng-selected它来匹配条件

工作演示

angular
.module('myApp', [])

// controller here
.controller('myCtrl', function($scope) {
    $scope.selectables = [
        { label: 'A', value: 1},
        { label:'B', value: 2},
        { label: 'C', value: 3}
    ];

    // this is the model that's used for the data binding in the select directive
    // the default selected item
    //using value, i want to set the selected value
    $scope.selectedItemvalue = "2"; 
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div ng-controller="myCtrl" ng-app="myApp">
    <select ng-model="selectedItemvalue">
        <option value="">--Select --</option>
        <option ng-repeat="sel in selectables" ng-selected="selectedItemvalue == sel.value" value="{{sel.value}}">{{sel.label}}</option>
    </select>
 <p>Selected Value is : {{selectedItemvalue}}</p>
</div>
Run Code Online (Sandbox Code Playgroud)


Sha*_*wal 7

您基本上需要对option标签使用以下语法(use ng-selected):

<option ng-repeat="sel in selectables" value="{{sel.value}}" ng-selected="sel.value == selectedItemvalue">{{sel.label}}</option>
Run Code Online (Sandbox Code Playgroud)