Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 ng选项未选择选项数组中的最后一项_Angularjs_Ng Options_Angularjs Ng Options - Fatal编程技术网

Angularjs ng选项未选择选项数组中的最后一项

Angularjs ng选项未选择选项数组中的最后一项,angularjs,ng-options,angularjs-ng-options,Angularjs,Ng Options,Angularjs Ng Options,我有一个奇怪的问题,我无法在Plunker上复制。当在模型中设置第一个选项“用户”时,select将正确显示该选项,并将其设置为“用户”。当我加载另一个角色为“admin”的用户时,select设置为blank选项 在我的控制器中,我定义: $scope.roles = ['user', 'admin']; 我的$scope.user对象如下所示: { "_id": "54f1f8f7e01249f0061c5088", "displayName": "Test1 User",

我有一个奇怪的问题,我无法在Plunker上复制。当在模型中设置第一个选项“用户”时,select将正确显示该选项,并将其设置为“用户”。当我加载另一个角色为“admin”的用户时,select设置为blank选项

在我的控制器中,我定义:

$scope.roles = ['user', 'admin'];
我的$scope.user对象如下所示:

{
   "_id": "54f1f8f7e01249f0061c5088",
   "displayName": "Test1 User",
   "provider": "local",
   "username": "test1",
   "__v": 0,
   "updated": "2015-03-02T07:41:42.388Z",
   "created": "2015-02-28T17:20:55.893Z",
   "role": "admin",
   "profileImageURL": "modules/users/img/profile/default.png",
   "email": "test1@user.com",
   "lastName": "User",
   "firstName": "Test1"
}
<select id="role" class="form-control ng-pristine ng-valid" data-ng-options="role for role in roles" data-ng-model="user.role">
    <option value="? string:joe ?"></option>
    <option value="0" selected="selected" label="admin">admin</option>
    <option value="1" label="user">user</option>
    <option value="2" label="joe">joe</option>
</select>
我认为:

<select id="role" class="form-control" data-ng-model="user.role" data-ng-options="role for role in roles"></select>

我发布了一个解决方案,但不是解决方案,也不是对上述问题的解释。如果任何人有发生这种情况的实际信息,以及如何使用阵列进行此操作的,请分享,我很乐意接受它作为答案

我将选项从简单数组更改为对象数组:

$scope.roles = [{name:'Admin',value:'admin'},{name:'User', value:'user'}];
我的看法是:

<select data-ng-model="user.role" data-ng-options="role.value as role.name for role in roles"></select>


感谢您的帮助和指导

我按照您的建议进行了所有组合,但没有出现错误

请查看我的测试:

选择
起初的

尝试验证您的angularjs版本。可能是特定的版本错误。

刚刚解决了这个问题。我的$scope.details.source模型正在由ajax填充,这导致了问题。(即,当ajax返回数组中的最后一项时,select不会初始化该项,其他值初始化为OK)。我确实找到了一份工作。我有

$scope.sources = ["1", "2", "3", "4", "5", "6", "7", "8"];
<select ng-model="details.source" ng-options="item for item in sources"> </select>
$scope.sources=[“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”];
并将其更改为:

$scope.sources = [{index:0, value:"1"}, {index:1, value:"2"}, {index:2, value:"3"}, {index:3, value:"4"}, {index:4, value:"5"}, {index:5, value:"6"}, {index:6, value:"7"}, {index:7, value:"8"}];
<select ng-model="details.source" ng-options="item.index as item.value for item in sources"> </select>
$scope.sources=[{index:0,value:“1”},{index:1,value:“2”},{index:2,value:“3”},{index:3,value:“4”},{index:4,value:“5”},{index:5,value:“6”},{index:6,value:“7”},{index:7,value:“8”};

我发现,与ajax相反,在控制器中初始化模型时,问题并不存在。唐诺为什么。。。但它是有效的

可能晚了,但这里有一个解决方案对我很有帮助。我总是添加一个虚拟的不可见选项,它是列表中的最后一个,并且从未被选中

<select class="form-control" 
        ng-options="r as r for r in myList | orderBy track by r"
        ng-model="item">
    <option ng-show="false"></option> <!-- TRICK: select last value too -->
</select>


好吧,正如你所说,这是基于给定的信息。尝试查看
选择
后的实际标记-您在最后一个选项中看到了什么?请查看有问题的标记更新记录?是的,谢谢链接。显然,当选项位于数组中时会发生这种情况。当您第一次加载页面时,
user.role
的初始值是多少?我的意思是当angular进行绑定时,实际上可以在顶部选择中重现错误。如果您检查html源代码,您将看到第一个空白选项:
我的用户对象是从API调用加载的,显然这是在绑定时模型不可用时发生的。奇怪的是,当选项位于对象中时,这不会发生。我扩展了plunker,包括基于对象的解决方法,并尝试使用setTimeout()模拟api调用。有趣的是,当您打开“旧”选择时,什么也没有发生,但只要您打开“对象”选择,它就会更新绑定。这与我的行为不完全相同,但它确实表明在角度处理数组和对象方面存在一些差异。请参见此处:关于第一次出现:为什么您的模型最初有一个空值,angularjs反映了这一点,使用这个初始空值创建了一个额外的值。当您选择另一个选项时,额外的选项将消失。如果需要,可以创建值为“”的选项的内部,以允许选择空值。关于第二次出现。。。您不应该直接在angularjs上使用setTimeout。您可以使用$timeout。我使用$timeout更改了您的示例,效果更好:-)是的,这是有意的,重点是在Angular的范围之外做一些事情,以演示Angular对简单数组和对象数组的不一致处理。也许有更好的展示,但你的演示帮助我缩小了这个现象!谢谢你的意见!:)
<select class="form-control" 
        ng-options="r as r for r in myList | orderBy track by r"
        ng-model="item">
    <option ng-show="false"></option> <!-- TRICK: select last value too -->
</select>