Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
选择Angularjs中的预选选项_Angularjs - Fatal编程技术网

选择Angularjs中的预选选项

选择Angularjs中的预选选项,angularjs,Angularjs,我已经试过书中的所有内容。我只是无法预先选择选项1 value=“1”用户。我正在使用angular网站。如果我删除ng model=“type”,它将被预选。但就是不能让它与ng model=“type”一起工作 使用者 广播公司 简单解决方案:您可以使用指令 <div class="form-group"> <select name="type" id="type" class="form-control" ng-model="type"> <

我已经试过书中的所有内容。我只是无法预先选择选项1 value=“1”用户。我正在使用angular网站。如果我删除ng model=“type”,它将被预选。但就是不能让它与ng model=“type”一起工作


使用者
广播公司

简单解决方案:您可以使用指令

<div class="form-group">
   <select name="type" id="type" class="form-control" ng-model="type">
      <option value="1" ng-selected="true">User</option>
      <option>Broadcasters</option>
   </select>
</div>

使用者
广播公司
发生这种情况是因为您将数字传递给ng模型,而输入的值是字符串。而且,如果您将字符串传递给ng模型,它将在没有ng selected指令的情况下工作

更好地使用:


$scope.types=[{
id:1,
标签:“用户”
}, {
id:2,
标签:“广播公司”
}];
$scope.currentType=$scope.types[0]//默认类型

您可以使用带有ng init的ng模型来设置默认选定值

<select name="type" ng-model="selected" ng-init="selected='1'" id="type" class="form-control" ng-model="type">

使用者
广播公司
使用数字选项值:


使用者
广播公司
类型={{type}}


这个问题是关于
AngularJS
还是
AngularJS
的?AngularJS会相应地标记它。我不知道怎么做。作为一个好的做法,
ng init
只能在非常必要的时候使用,即:在
ng repeat
中初始化/别名某个东西。在这种简单的情况下,没有必要使用
nginit
。检查。另外,您在
中有两个
ng模型
(在两个标记中)。
<select name="type" ng-model="selected" ng-init="selected='1'" id="type" class="form-control" ng-model="type">