how to use ng-option to set default value of select element

前端 未结 12 644
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 11:07

I\'ve seen the documentation of the Angular select directive here: http://docs.angularjs.org/api/ng.directive:select. I can\'t figure how to set the default value. This is

12条回答
  •  粉色の甜心
    2020-11-22 12:01

    The angular documentation for select* does not answer this question explicitly, but it is there. If you look at the script.js, you will see this:

    function MyCntrl($scope) {
      $scope.colors = [
        {name:'black', shade:'dark'},
        {name:'white', shade:'light'},
        {name:'red', shade:'dark'},
        {name:'blue', shade:'dark'},
        {name:'yellow', shade:'light'}
      ];
      $scope.color = $scope.colors[2]; // Default the color to red
    }
    

    This is the html:

    
    

    This seems to be a more obvious way of defaulting a selected value on an

    提交评论

提交回复
热议问题