gpt4 book ai didi

javascript - Angular 不会使用 ng-model 更新选择元素

转载 作者:行者123 更新时间:2023-12-02 17:59:45 24 4
gpt4 key购买 nike

Angular 不会使用 ng-model 的内容更新选择元素。绑定(bind)是单向的,只能选择->模型。如何将模型中的更改传播到 View ?

模板:

<select class="gu2" name="coverDuration" ng-model="creditLimit.coverDuration" required="required">
<option ng-value="90" selected="selected">90 days</option>
<option ng-value="180">180 days</option>
</select>
just to check: {{creditLimit.coverDuration}}

Controller :

app.controller('CreditLimitDetailsController', function ( $scope, creditLimit /* ... */ ) {
// creditLimit.coverDuration can be 90 or 180.
// I use a service to store the values between successive instanciations
$scope.creditLimit = creditLimit;
}

在此示例中,选择保持无选项选择,而检查显示 90。

最佳答案

它有效:

app.controller('MainCtrl', function($scope) {
$scope.creditLimit = {}; //needs to be initialized
$scope.creditLimit.coverDuration = 90;
});

并使用

value="" 

相反

ng-value=""

Plunker

关于javascript - Angular 不会使用 ng-model 更新选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20656268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com