简体   繁体   English

AngularJS:根据对第一个选择下拉列表的选择来填充第二个选择下拉列表

[英]AngularJS: Populate second select-dropdown based on choice of first select-dropdown

I am trying to populate second select-dropdown based on choice of first select-dropdown in angularJS. 我正在尝试根据angularJS中的第一个选择下拉列表填充第二个选择下拉列表。 I don't know why it doesn't update the DOM (UI): 我不知道为什么它不更新DOM(UI):

Dropdown 1 下拉菜单1

<div class="input-field col s12 m4 l4 xl4">
        <select ng-model="selectedMacroProdotti.fdlcatprod.l01CatPrd" name="l02CatPrd" class="validate" required 
            ng-options="item.l01CatPrd as (item.l01CatPrd | uppercase) for item in listCategoriaProdotti"
            ng-change="populateTipoProdottoListino()">
            <!--  -->
        </select> <label>Codice Categoria Prodotto*</label>
        <div role="alert">
                <span class="error" ng-show="ModificaMacroProdotto.l02CatPrd.$error.required"><font color="red">Campo Obbligatorio!</font></span>           
        </div>
    </div>

Dropdown 2 下拉菜单2

<div class="input-field col s12 m4 l4 xl4">
    <select ng-model="selectedMacroProdotti.fdltprdlis.l39TipPrdLst" name="l02TipPrdLst" class="validate"
            ng-options="item.l39TipPrdLst as (item.l39TipPrdLst | uppercase) for item in listTipoProdottoListinoFiltered">
            </select> <label>Tipo Prodotto Listino</label>
    </div>

In the dropdown 1 when the value changes i invoke with ng-change the function "populateTipoProdottoListino()": 在下拉列表1中,当值更改时,我使用ng-change调用函数“ populateTipoProdottoListino()”:

var ctrlMacroProdotti = function(macroProdottiService, $scope, $window, macroProdottiFactory, $uibModal, dateFormatService, listCategoriaProdotti, listTipoProdottoListino, listTipoFrontEndV){`$scope.populateTipoProdottoListino = function(){
        $scope.listTipoProdottoListino = listTipoProdottoListino;
        $scope.listTipoProdottoListinoFiltered = [];
        var currentCatProd = $scope.selectedMacroProdotti.fdlcatprod.l01CatPrd;

        $scope.listTipoProdottoListino.forEach(function(item,index){
            if(item.l39CatPrd === currentCatProd){
                $scope.listTipoProdottoListinoFiltered.push(item);
            }
        })
    }
}

The list listTipoProdottoListinoFiltered is populated with values but nothing happens in the front-end. 列表listTipoProdottoListinoFiltered填充了值,但前端未发生任何事情。 I used also $scope.$apply() but it doesn't fix my problem. 我还使用了$ scope。$ apply(),但是它不能解决我的问题。

Can you help me? 你能帮助我吗?

Thank you. 谢谢。

我已经为您的问题创建了一个小提琴,如果无法正常工作,请尝试在您的代码中进行尝试,如果您的代码无法正常更新,我们可以一起调试

[http://jsfiddle.net/swatrahul/Xku9z/504/][1]

I solved the issue. 我解决了这个问题。 The problem was Angular Materialize ( http://materializecss.com/forms.html#select-initialization ): 问题是Angular Materialize( http://materializecss.com/forms.html#select-initialization ):

If you want to update the items inside the select, just rerun the initialization code from above after editing the original select. 如果要更新选择内的项目,只需在编辑原始选择后从上方重新运行初始化代码即可。 Or you can destroy the material select with this function below, and create a new select altogether 或者您可以使用下面的此功能销毁材料选择,然后创建一个新的选择

$('select').material_select('destroy'); $( '选择')material_select( '摧毁')。

My updated code is this : 我更新的代码是这样的:

$scope.populateTipoProdottoListino = function(){
        $scope.listTipoProdottoListinoFiltered = [];
        var currentCatProd = $scope.selectedMacroProdotti.fdlcatprod.l01CatPrd;
        $scope.listTipoProdottoListino.forEach(function(item,index){
            if(item.l39CatPrd === currentCatProd){
                $scope.listTipoProdottoListinoFiltered.push(item);
            }
        });
        setTimeout(() => {
            $('#l02TipPrdLst').material_select('destroy');
            $('#l02TipPrdLst').material_select();
            $scope.$apply();
        }, 10);

    }

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

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