Set default value in select when the ng-model is null

前端 未结 8 2159
悲哀的现实
悲哀的现实 2021-02-05 10:33

I\'m new to angular and i\'m trying to set a default state to select when my ng-model is null. Basically I want to set it to \'None\' if the model is empty. I tried the below co

8条回答
  •  名媛妹妹
    2021-02-05 11:11

    H i ,

    My thoughts are it is best to do this in the app rather than the template.

      if(typeof $scope.item.carType==="undefined") {
         $scope.item.carType="none";
      }
    

    or simply setting the value

    $scope.item.carType="none";
    

    before it is updated with whatever you are using to set the value : -

    $scope.item.carType="none";
    $scope.item.carType=someasyncfunctionthatmighttakeawhileandthetemplateisrenderedbeforeitarrives();
    

提交回复
热议问题