Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当ng模型为空时,在select中设置默认值_Javascript_Angularjs - Fatal编程技术网

Javascript 当ng模型为空时,在select中设置默认值

Javascript 当ng模型为空时,在select中设置默认值,javascript,angularjs,Javascript,Angularjs,我是angular的新手,我正在尝试设置一个默认状态,以便在我的ng模型为null时进行选择。基本上,如果模型为空,我想将其设置为“无”。我尝试了下面的代码,但它不起作用 <select ng-init="item.carType | item.carType='none'" ng-model="item.carType"> <option value="none">None</option> <option value="manual"

我是angular的新手,我正在尝试设置一个默认状态,以便在我的ng模型为null时进行选择。基本上,如果模型为空,我想将其设置为“无”。我尝试了下面的代码,但它不起作用

<select ng-init="item.carType | item.carType='none'" ng-model="item.carType">
    <option value="none">None</option>
    <option value="manual">Manual</option>
    <option value="auto">Auto</option>                      
</select>

没有一个
手册
自动的

我建议您这样做。 在控制器中创建一个函数,并检查
$scope.item.carType==undefined
是否分配默认值

$scope.setDefaultValueForCarType = function () {
      if($scope.item.carType == undefined) {
          $scope.item.carType = "none";
      }
}
这也行

<select ng-init="item.carType='none'" ng-model="item.carType">

hi

我的想法是,最好在应用程序中而不是在模板中执行此操作

  if(typeof $scope.item.carType==="undefined") {
     $scope.item.carType="none";
  }
或者简单地设置值

$scope.item.carType="none";
在使用您正在使用的设置值进行更新之前:-

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

可以使用ng init中的值进行求解

<select ng-init="item.carType='none'" ng-model="item.carType">
    <option value="none">None</option>
    <option value="manual">Manual</option>
    <option value="auto">Auto</option>                      
</select>

没有一个
手册
自动的

您要查找的内容未定义

如果该值未定义,则在选择“无”时是否需要一个值才能转到数据库

如果发送空的是可以接受的,你可以考虑以下内容:

<select ng-model="item.carType">
    <option value="">None</option>
    <option value="manual">Manual</option>
    <option value="auto">Auto</option>                      
</select>

没有一个
手册
自动的

这也允许您传入一个值,并使用相同的表单进行编辑和新记录。

尝试以下操作:

<select ng-init="item.carType = item.carType || 'none'" ng-model="item.carType">
    <option value="none">None</option>
    <option value="manual">Manual</option>
    <option value="auto">Auto</option>                      
</select>

没有一个
手册
自动的

也就是说,这可能是对
ngInit
的滥用。正确的方法是使用控制器(或服务,如果是从那里来的话)中的正常值初始化模型。

由于错误的情况,您的
ng model=“item.carType”
经常与任何选择选项键不匹配

例如:
item.cartType
“Manual”
,但在选项中,您有一个选项
Manual
,在这种情况下,它不起作用,在控制台输出中,您会看到类似这样的内容:

因此,请更正数据模型或选择选项值:

手册

我对您的代码做了以下更改,现在应该可以使用了。我删除了
ng init=”“
中的第二部分,并将None选项值设置为
undefined

<select ng-init="item.carType='none'" ng-model="item.carType">
    <option value=undefined>None</option>
    <option value="manual">Manual</option>
    <option value="auto">Auto</option>                      
</select>

没有一个
手册
自动的

基于Rob Sedgwick的想法,在紧要关头,我不使用
$scope
所以使用controller作为语法

loadStates(); //this calls my function for my code

// this is based on what Rob S. is doing with $scope    
if(typeof vm.scriptQuestion.statecode ==="undefined") {
    console.log('in');
    vm.scriptQuestion.statecode = "AA";  // "AA" happens to be the value of the default name for All of my States that is then displaying "ALL" now when before it was blank
}
这是我的选择(是的,这是做角度选择框的一种垃圾方式,因为ng repeat对于选择是非常有限的)


{{state.StateName}

感谢您的回复。我们考虑过这一点,但想尝试ng init way.cardeol这适用于表单为空的情况,否则如果item.carType已经有一个值,它仍然默认为none。null作为实际值计算,OP可能表示未定义?这是什么意思
item.carType=item.carType
?如果它是一个“truthy”值,则将
item.carType
分配给它自己,否则它将把字符串“none”分配给
item.carType
。因此,如果
item.carType
已经有一个值,则不会发生任何更改,否则-由于
|
,它将被分配给字符串值“none”。不要这样做。这会干扰验证。如果您有一个
required
标志,将默认值设置为
'none'
将使该值“设置”,并且不会触发
required
错误。感谢您指出这是不鼓励的。在我的例子中,我在由
ng if
创建的嵌套作用域中使用它,该作用域不能被我的控制器的作用域修改
 <select id="States" ng-model="vm.scriptQuestion.statecode">
     <option ng-repeat="state in vm.states" value="{{state.StateCode}}">{{state.StateName}}</option>
 </select>