简体   繁体   English

未在页面刷新上设置角度选择的选定值

[英]Selected value for angular select not be set on page refresh

The selected value of my angularjs select is not being set on page refresh. 我的angularjs select的选定值未在页面刷新上设置。

  • It is set if I navigate to a different view and then navigate back again. 如果我导航到其他视图,然后再次导航回去, 则会设置它。
  • The model for the select is saved to local storage and is initialized into the scope onload. 选择的模型将保存到本地存储,并初始化为作用域onload。
  • I have checked that the data in the scope is the same in the two situations of navigating away and back again, and refreshing the page. 在两种情况下,我已经检查了范围中的数据是否相同:导航和返回,以及刷新页面。 The data is exactly the same. 数据完全相同。

After a lot of investigation, I see that when the select is pristine, or when the page is refreshed , the select list will have an extra blank option, like first one below with value ? 经过大量调查,我发现当选择为原始数据或刷新页面时 ,选择列表将有一个额外的空白选项,例如下面的第一个带有值的? :

<select ng-model="question.answer" class="form-control" ng-options="opt as opt.value for opt in question.options" >
    <option value="?" selected="selected"></option>
    <option value="0">1</option>
    <option value="1">2</option>
</select>

Note The above HTML is the output. 注意上面的HTML是输出。 My Angular view just has: 我的角度视图只有:

<select ng-model="question.answer" class="form-control" ng-options="opt as opt.value for opt in question.options" />

I'm not sure why this would be preventing the correct option from being selected as the correct object is still present at question.answer. 我不确定为什么这会阻止选择正确的选项,因为问题仍然存在于question.answer。 See a simplified version of the model below: 请参阅以下模型的简化版本:

var question = {
    options: [
        {
            id: 1,
            value: "1"
        },
        {
            id: 2,
            value: "2"
        }
    ],
    answer: {
        id: 2,
        value: "2"
    }
};

My understanding is that in ng-options , the first opt specifies that this is the value that should be assigned to the variable specified in ng-model . 我的理解是,在ng-options ,第一个opt指定这是应该分配给ng-model指定的变量的值。 In other words, I will be assigning an object in the options array to question.answer , rather than just the value. 换句话说,我将在options数组中分配一个对象给question.answer ,而不仅仅是值。 I can see by looking at the model that this is the case. 通过查看模型可以看到情况确实如此。

But I'm not sure how Angular will use the value in question.model to determine which option is set as selected. 但是我不确定Angular将如何使用question.model的值来确定哪个选项设置为选中状态。

I wonder if the problem is something to do with the fact that the values of the options are set as indices of question.options and it can't work out from question.answer which index that answer is. 我想知道问题是否与以下事实有关:选项的值设置为question.options的索引,并且不能从question.answer中找出答案的索引。

So, I guess my questions are: - Why is the correct option not being set when this extra <option value="?" selected="selected"></option> 因此,我想我的问题是:-为什么当这个额外的<option value="?" selected="selected"></option>时,没有设置正确的选项<option value="?" selected="selected"></option> <option value="?" selected="selected"></option> is present? <option value="?" selected="selected"></option>存在吗? - How does Angular determine how to use the model to set the selected option (aka - what are those funny indices in the option values?) -Angular如何确定如何使用模型来设置选定的选项(又名-选项值中那些有趣的索引是什么?)

Sorry this post is so long. 抱歉,这篇文章太长了。 Thank for reading. 感谢阅读。

Update: Using the Chrome AngularJS Batarang extension, that the value being set to question.answer for a picklist is eg: 更新:使用Chrome AngularJS Batarang扩展程序,将选择列表设置为question.answer的值例如:

 { 
    $ref: $["options"][1]
 }

which will means the value of the picklist is "2". 这意味着选择列表的值为“ 2”。

So that is how it is using the indices of the options are being used, but is that how I have to set my model? 这就是使用选项索引的方式,但这就是我必须设置模型的方式吗? ie using the $ref etc? 即使用$ref等?

I edited the answer after I looked at what you were doing. 在查看您的操作后,我编辑了答案。

The ng-options attribute has a track by expression to do just what you're talking about: ng-options属性具有一个表达式表达式,可以完成您正在谈论的事情:

ng-options="opt as opt.Value for opt in question.options track by opt.id"

See the updated fiddle: http://jsfiddle.net/CkLEu/6/ 请参阅更新的小提琴: http : //jsfiddle.net/CkLEu/6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM