简体   繁体   English

如何在具有动态数据的html标签中强制选择值

[英]How to force selected value in select html tag with dynamic data

I'm trying to force a selected value of a select field using AngularJS 我正在尝试使用AngularJS强制选择字段的选定值

To provide a little bit more of background, im using AngularJS. 为了提供更多背景信息,我使用AngularJS。

My code would look like that: 我的代码如下所示:

<select class="form-control" name="SOExample" 
    ng-model="$ctrl.variableValue"
    ng-change="$ctrl.functionThatControlsValidations()"
    ng-click="$ctrl.updateListOfValues()">

    <option ng-repeat="value in $ctrl.listOfValues" value="{{value}}">
        {{value}}
    </option>
</select>

So to explain a little bit: 因此,请解释一下:

  • "$ctrl.listOfValues" is the list of values that is updated each time a ng-click is triggered. “ $ ctrl.listOfValues”是每次触发ng-click时都会更新的值列表。 For instance, it's an array of integer values. 例如,它是一个整数值数组。
  • "$ctrl.variableValue" holds the value of the desired value to be initially selected from the "$ctrl.listOfValues" and it's always the same except when the value selected in the field is changed and then it is updated to that new value. “ $ ctrl.variableValue”保存要从“ $ ctrl.listOfValues”中初始选择的所需值的值,并且除更改在字段中选择的值然后将其更新为该新值外,该值始终相同。
  • "$ctrl.updateListOfValues()" simply updates the list of values. “ $ ctrl.updateListOfValues()”仅更新值列表。

My JS code would look like this: 我的JS代码如下所示:

function ExampleController(...){
    this.$onInit = function() {
        this.variableValue = 16;
        this.listOfValues = [];
        this.listOfValues.push(this.variableValue);
    }

    this.updateListOfValues = function(){
        this.listOfValues = [];
        this.addElementsToListOfValues(); //Value of "this.varibleValue" will always be in the array of values
    }
}

The problem comes when i want the value of "variableValue" to be the one selected in the field. 当我希望“ variableValue”的值成为该字段中选择的值时,问题就来了。 If the "listOfValues" is empty no value will be shown at the beginning. 如果“ listOfValues”为空,则开头将不显示任何值。 So to solve it at the $onInit i forced the varibleValue on the list. 因此,要在$ onInit上解决它,我将varibleValue强制在列表中。

Then when I click on the select field and it refreshes the "listOfValues" the values that is selected will change to the first element of the refreshed list, but i want it to be still, the value of variableValue, until the user select another one, if it does. 然后,当我单击选择字段并刷新“ listOfValues”时,所选择的值将更改为刷新列表的第一个元素, 但我希望它保持不变,即variableValue的值,直到用户选择另一个(如果有)。

I'm not sure if it exactly what you wanted, but I designed a plunker example for you 我不知道如果你想要什么,但我设计了一个plunker例子给你

<select ng-model="$ctrl.variableValue"
        ng-change="$ctrl.functionThatControlsValidations()"
        ng-click="$ctrl.updateListOfValues()">

    <option ng-repeat="value in $ctrl.listOfValues" ng-value="value">
        {{value}}
    </option>
</select>

The point is to use ng-value="value" instead of value="{{value}}" . 重点是使用ng-value="value"而不是value="{{value}}" The value is not being selected because you compare int number, stored in ng-model with string value, stored in option attribute. 由于您将存储在ng-model中的int数字与存储在option属性中的字符串值进行比较,因此未选择该值。

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

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