对角选定值选择不刷新页面设置 [英] Selected value for angular select not be set on page refresh

查看:119
本文介绍了对角选定值选择不刷新页面设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我angularjs的选定值选择未在页面刷新设置。

The selected value of my angularjs select is not being set on page refresh.


  • 如果我浏览到一个不同的看法,然后再导航回是设置。

  • 的选择模型保存到本地存储和被初始化为范围的onload。

  • 我已签,在范围内的数据是在航行路程,然后再返回,并刷新页面的两种情况是一样的。的数据是完全一样的。

经过大量的调查,我看到,当选择是质朴的,或当页面刷新后,选择列表中会有一个额外的空白选项,就像值低于第一个<$ ?C $ C> :

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>

注意的上面是HTML的输出。我斜角视图只是有:

Note The above HTML is the output. My Angular view just has:

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

我不知道为什么会是preventing被选为正确的对象仍然是present在question.answer正确的选项。请参见下面的模型的简化版本:

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. See a simplified version of the model below:

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

我的理解是,在 NG-选项,第一个选择规定,这是应该的价值分配给指定的变量 NG-模型。换句话说,我会分配方案阵列中的一个对象 question.answer ,而不仅仅是值。我可以通过查看模型,这是这种情况看。

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. In other words, I will be assigning an object in the options array to question.answer, rather than just the value. I can see by looking at the model that this is the case.

但我不知道如何将角中的值 question.model 来确定哪个选项的选择设置。

But I'm not sure how Angular will use the value in question.model to determine which option is set as selected.

我不知道这个问题是事做与选项的值设置为question.options的指标,它不能从 question.answer 这个问题的答案是指数。

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.

所以,我想我的问题是:
 期权价值=; - 为什么是正确的选项没有被当这个额外的&LT设置?选择=选择&GT;&LT; /选项&GT; 是present?
  - 如何角确定如何使用模型来设置选项(又名 - 那些是什么好笑指数在选项值?)

So, I guess my questions are: - Why is the correct option not being set when this extra <option value="?" selected="selected"></option> is present? - How does Angular determine how to use the model to set the selected option (aka - what are those funny indices in the option values?)

对不起,这个职位是这么久。感谢您阅读。

Sorry this post is so long. Thank for reading.

更新:
使用Chrome AngularJS Batarang延伸,该值被设置为 question.answer 的领料单是例如:

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

,这将意味着在选择列表的值是2。

which will means the value of the picklist is "2".

所以这是它是如何使用的正在使用的选项的索引,但我怎么也设置我的模型?即使用 $ REF 等?

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? i.e. using the $ref etc?

推荐答案

我编辑的答案我看着你在做什么了。

I edited the answer after I looked at what you were doing.

该NG选项属性有由前pression的轨道,只是你在说什么:

The ng-options attribute has a track by expression to do just what you're talking about:

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

请参阅更新的提琴: http://jsfiddle.net/CkLEu/6/

这篇关于对角选定值选择不刷新页面设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆