ng-selected不更新下拉列表角度

时间:2018-03-02 11:02:20

标签: javascript html angularjs angularjs-ng-repeat

我一直在使用AngularJS,在执行以下功能时,我仍然得到一个空选项作为select元素的第一个子项。

这是我的HTML代码段

<select id="connectTV" aria-label="How many Dish TVs" ng-model="connectTVs.index" ng-change="onChangeMethod(connectTVs.index)" class="selector" aria-required="true">
           <option  ng-repeat="noOfTV in noOfTVs" ng-value="noOfTV.index" ng-selected="noOfTV.index == connectTVs.index">{{noOfTV.value}}</option>
</select>

JS代码

$scope.noOfTVs = [
                      {index: 0, value: 'How many Dish TVs?'},
                      {index: 1, value: 'Connect 1 Dish TV'},
                      {index: 2, value: 'Connect 2 Dish TVs'},
                      {index: 3, value: 'Connect 3 Dish TVs'},
                      {index: 4, value: 'Connect 4 Dish TVs'},
                      {index: 5, value: 'Connect 5 Dish TVs'},
                      {index: 6, value: 'Connect 6 Dish TVs'},
                      {index: 7, value: 'Connect 7 Dish TVs'},
                      {index: 8, value: 'Connect 8 Dish TVs'}
                      ];


$scope.onChangeMethod = function(connectTVsIndex) { 

    // Doing some actions
     $scope.connectTVs = $scope.noOfTVs[connectTVsIndex];
     $window.sessionStorage.setItem("noOfTVSelected", $scope.connectTVs.index);
}

最后,这里生成的HTML

<option value="? number:4 ?"></option>

enter image description here

将空选项作为select元素的第一个子元素。哪里我做错了?有人可以为此提供帮助吗?

2 个答案:

答案 0 :(得分:1)

您可以将// api/shopping_lists/1 { userId: 43 weeks: { 19-03-2018: ['Eggs', 'Milk'] // week 1 26-03-2018: ['Pasta', 'Cheese'] // week 2 } } 用于选择而不是ng-options。前者对于更大的数组有更好的performance,并提供更大的灵活性。

&#13;
&#13;
ng-repeat
&#13;
var app = angular.module('myApp', [])
app.controller('myCtrl', [
    '$scope',
    function($scope) {
        $scope.noOfTVs = [{
            index: 0,
            value: 'How many Dish TVs?'
        }, {
            index: 1,
            value: 'Connect 1 Dish TV'
        }, {
            index: 2,
            value: 'Connect 2 Dish TVs'
        }, {
            index: 3,
            value: 'Connect 3 Dish TVs'
        }, {
            index: 4,
            value: 'Connect 4 Dish TVs'
        }, {
            index: 5,
            value: 'Connect 5 Dish TVs'
        }, {
            index: 6,
            value: 'Connect 6 Dish TVs'
        }, {
            index: 7,
            value: 'Connect 7 Dish TVs'
        }, {
            index: 8,
            value: 'Connect 8 Dish TVs'
        }];
        $scope.connectTVs=$scope.noOfTVs[0];
        $scope.onChangeMethod = function() {
            // Doing some actions
            console.log($scope.connectTVs)
        }

    }
])
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这是因为您ngModel上的初始绑定select未定义。

ng-model="connectTVs.index $ scope.connectTVs不存在!

适合您的问题的最快解决方案是添加:

$scope.connectTVs = {
    index: 0, 

$ scope.onChangeMethod = function(connectTVsIndex){

// Doing some actions
 $scope.connectTVs = $scope.noOfTVs[connectTVsIndex];
 $window.sessionStorage.setItem("noOfTVSelected", $scope.connectTVs.index);

} 最后,这是生成的HTML

在此处输入图像描述

将空选项作为select元素的第一个子元素。我做错了什么?有人可以为此提供帮助吗?

    value: 'How many Dish TVs?'
}

但我想补充一点,在这种对象中添加索引属性是没用的,因为你可以使用$index在视图中访问它。

工作演示:https://plnkr.co/edit/mmD4mVUNxLr9sbojrCKc?p=preview