gpt4 book ai didi

javascript - 当使用 ng-init 初始化模型时,默认选择在 ngoptions 中不起作用

转载 作者:行者123 更新时间:2023-11-29 18:10:22 24 4
gpt4 key购买 nike

我有一个级联选择,第二个下拉菜单基于第一个下拉菜单选择出现。出现一个额外的空白选项,这不是下拉列表预期行为的一部分。

<select ng-init="order.attempt_status_sub = order.attempt_status_sub || subStatuses[0].name" 
data-ng-model="order.attempt_status_sub" ng-options="subStatus.name as
subStatus.name for subStatus in subStatuses">
</select>

如何避免空的额外选择出现在下拉列表中?

enter image description here

我的级联下拉菜单的代码是

    <div class="form-group" ng-class="{ 'has-error' : submitted && orderForm.content.$invalid}">
<div class="controls">
<select ng-change="getSubStatuses(order.attempt_status)" data-ng-model="order.attempt_status" ng-options="status.name as status.name for status in statuses">
</select>
</div>
</div>

<div class="form-group" ng-show="subStatuses.length" ng-class="{ 'has-error' : submitted && orderForm.content.$invalid}">
<div class="controls">
<select ng-init="order.attempt_status_sub = order.attempt_status_sub || subStatuses[0].name" data-ng-model="order.attempt_status_sub" ng-options="subStatus.name as subStatus.name for subStatus in subStatuses">
</select>
</div>
</div>
$scope.getSubStatuses = function(attempt_status) {

var statuses = $scope.statuses;
for(var index in statuses) {
if(statuses[index].name === attempt_status) {

if(statuses[index].children) {
$scope.subStatuses = statuses[index].children;
} else {
$scope.subStatuses = [];
}
break;
}
}
};

最佳答案

我假设您正在为异步绑定(bind)数据设置此项。 ng-init 对于这个目的来说真的很糟糕。拇指规则是不要将 ng-init 用于 Controller 应该做的事情。您应该在 Controller 中设置 View 模型。异步绑定(bind)数据时它可能失败的原因是因为未监视 ng-init 的表达式。所以它只在第一次渲染期间运行一次并且 subStatuses 尚未填充并且它回退到显示默认的空选项。即使在稍后的摘要循环中填充了数据并更新了 View ,ng-init 表达式也不会再次运行。

From Doc

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

所以只需在您的 Controller 中初始化它,而不是使用 ng-init。

$scope.getSubStatuses = function(attempt_status) {

var statuses = $scope.statuses;
for(var index in statuses) {
if(statuses[index].name === attempt_status) {

if(statuses[index].children) {
$scope.subStatuses = statuses[index].children;
} else {
$scope.subStatuses = [];
}
break;
}
}
//Initialize the model here, assuming $scope.order is already initialized
$scope.order.attempt_status_sub = ($scope.subStatuses[0] ||{}).name;
};

angular.module('app', []).controller('ctrl', function($scope, $timeout) {

$timeout(function() {
$scope.subStatuses = [{
name: 'test1'
}, {
name: 'test2'
}, {
name: 'test3'
}];

$scope.order = {
attempt_status_sub1: $scope.subStatuses[0].name
};

});



});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
With Ng-Init
<select ng-init="order.attempt_status_sub = order.attempt_status_sub || subStatuses[0].name" data-ng-model="order.attempt_status_sub" ng-options="subStatus.name as
subStatus.name for subStatus in subStatuses">
</select>

With controller initialization.

<select data-ng-model="order.attempt_status_sub1" ng-options="subStatus.name as
subStatus.name for subStatus in subStatuses">
</select>
</div>

关于javascript - 当使用 ng-init 初始化模型时,默认选择在 ngoptions 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27909173/

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