Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.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所选元素未在“选择”元素中工作_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript ng所选元素未在“选择”元素中工作

Javascript ng所选元素未在“选择”元素中工作,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个选择 <select ng-model="collegeSelection" ng-options="c as c.CollegeName for c in colleges" ng-selected="c.CollegeName == collegeSelection.CollegeName" name="selectCollege" id="selectCollege"></select> 但是,当c.CollegeName==collegeSele

我有一个选择

<select ng-model="collegeSelection" ng-options="c as c.CollegeName for c in colleges" ng-selected="c.CollegeName == collegeSelection.CollegeName" name="selectCollege" id="selectCollege"></select>  


但是,当c.CollegeName==collegeSelection.CollegeName两者都匹配时,仍不会选择该项。文档似乎没有帮助。有什么想法吗

ng选择的
应在
标记中使用,而不是在
标记中使用。仔细看看它和示例

因为
select
指令基于
ngModel
确定所选选项。因此,一旦删除
ng selected=“c.CollegeName==collegeSelection.CollegeName”
,您的代码应该可以工作

我创建了一个非常简单的示例来演示
select
指令中的“selected”功能

更多详情: AngularJS使用
ngModel
指令在模型和UI元素之间启用“双向数据绑定”

在“选择”的情况下,您指定为
的模型
collegeSelection
是所选项目。这意味着如果用户从页面上的下拉列表中选择一个项目,
collegeSelection
将设置为该项目,如果将
collegeSelection
设置为javascript代码中的某个项目,AngularJS将确保选中对应的

假设控制器中有以下代码:

$scope.colleges = [
    {id: 0, name: 'a'},
    {id: 1, name: 'b'},
    {id: 2, name: 'c'}
];

$scope.collegeSelection = $scope.colleges[0];
HTML看起来像:

<select ng-model="collegeSelection" ng-options="c as c.name for c in colleges"></select>

就这样!如果运行代码,将选择学院数组中的第一个学院

请记住,
collegeSelection
是选中的选项,无论是因为用户在UI上选择了一个项目,还是您在javascript中选择了一个项目


这就是双向数据绑定的工作原理。

在使用
ng selected
玩了一段时间后,我无法按照您的要求让它工作。但是,我可以使用
nginit
预先选择一个特定选项

这是我的一个解决方案。我的
最终是:

 <select ng-model="selectedColor" ng-options="color.value as color.name for color in colors" ng-init="selectedColor='yellow'">
   <option value="">Select A Color</option>
 </select>`

ng init=“selectedColor='yellow'”
更改为另一个值将选择不同的选项。

有些人对此有问题。我找到了一个很好的解决方案,可以使用一个简单的下拉菜单if
controller as someController

var vm = this;
this.colors = [
        {name:'Red'}, 
        {name:'Orange'}, 
        {name:'Yellow'}, 
        {name:'Green'}, 
        {name:'Blue'}, 
        {name:'Indigo'}, 
        {name:'Violet'}
 ];
this.color_selected = "Yellow";
<select ng-model="someController.color_selected" ng-options="opt.name as opt.name for opt in someController.colors">
</select>
var vm=this;
此项。颜色=[
{名称:'Red'},
{名称:'Orange'},
{name:'Yellow'},
{name:'Green'},
{name:'Blue'},
{名称:'Indigo'},
{名称:'Violet'}
];
this.color\u selected=“Yellow”;

`

我遇到了类似的问题,并意识到原因是数据类型不同
ng model
正在与字符串值进行比较,但我正在从数据库中提取一个整数,因此它不会自动选择该选项。为了克服这个问题,在查询数据库中的数据后,我对整数调用了
toString()
,以确保数据类型匹配。

我明白了,但我试图做的是使select元素中的某个项成为选中的元素。怎么做呢?你只需要设置
collegeSelection
模型。你不一定需要
ngInit
。尝试添加
$scope.selectedColor='yellow'
$scope.colors=[…]之后
var vm = this;
this.colors = [
        {name:'Red'}, 
        {name:'Orange'}, 
        {name:'Yellow'}, 
        {name:'Green'}, 
        {name:'Blue'}, 
        {name:'Indigo'}, 
        {name:'Violet'}
 ];
this.color_selected = "Yellow";
<select ng-model="someController.color_selected" ng-options="opt.name as opt.name for opt in someController.colors">
</select>