如何在AngularJS中动态添加行来选择/下拉

时间:2015-06-23 10:44:48

标签: javascript angularjs select html-select

我有一个从DB填充的returnreasons的选择下拉列表。如果用户具有特定角色,则可以添加新原因。问题是我的下拉模型在添加后没有自动更新?虽然添加到DB,但在视图中,即使模型已更改,也不会再次填充下拉列表。

<select class="form-control" ng-model="selectedReason">
    <option ng-selected="{{reason.returnreasonId === selectedReason}}"
        ng-repeat="reason in returnreasons"
        value="{{reason.returnreasonId}}">
        {{reason.returnText}}
    </option>
</select>

<div class="form-group form-group-lg" ng-show="addReasonToggle">

    <label class="col-md-2 control-label">New reason:</label>
    <div class="col-md-8">
        <input type="text" class="form-control" ng-model="returnReason.returnText" placeholder="New reason for return">
    </div>
    <div class="col-md-2">
        <button class="btn btn-small btn-primary" ng-click="addNewReturnReason(returnReason)">Lisää</button>
    </div>
</div>

在控制器中

$scope.addNewReturnReason = function(returnReason){
                    var savedReturnReason = [];
                    if (returnReason === undefined || returnReason === null) {
                        console.log("returnReason null");
                    } else {
                        // This is default value
                        returnReason.languageLanguageId = $scope.languages[0];
                        savedReturnReason = returnReasonSvc.save({}, returnReason);
                        savedReturnReason.$promise.then(function (result) {
                            $scope.returnReason = result;
                            $scope.returnreasons = returnReasonSvc.query();
                            $scope.addReasonToggle = false;
                            $scope.selectedReason=savedReturnReason;
                        });
                    };

                }

1 个答案:

答案 0 :(得分:-1)

可能是观看周期未运行的问题您可以在然后功能中尝试此操作..

$scope.$apply();

手动运行.hope以便它可以工作。