繁体   English   中英

角度填充数据以下拉

[英]Angular populate data to drop down

我有json格式的返回数据,如何使用ng-model将数据加载回下拉列表作为默认值?

<div class="col-md-3 col-md-offset-2">
            <h5 class="over-title">Category</h5>
            <ui-select ng-model="product.categories.selected" theme="bootstrap">
                <ui-select-match placeholder="Select Category" ng-model="product.name">
                    {{$select.selected.name}}
                </ui-select-match>
                <ui-select-choices repeat="item in categories | filter: $select.search">
                    <div ng-bind-html="item.name | highlight: $select.search"></div>
                </ui-select-choices>
            </ui-select>
        </div>

json如下:

Object { category: "Sightseeing & Tours" }

当前的ng-model=product.name用于在创建新产品时填充选项。 现在,当他们重新编辑表单时,下拉列表应列出先前保存的数据,这些数据由json

创建新产品时的下拉列表选项功能:

$scope.categories = function(){
       Account.getCategoryList()
        .then(function(response){

            $scope.categories = response.data;
        })


        .catch(function(response){
            console.log(response.data.messages);
        });

    };

当有人重新编辑表单时返回json那个

$scope.produk = {};
    $scope.getProductToEdit = function(id){
      Account.getProductToEdit(id)
        .then(function(response){

            $scope.produk = response.data.product;
            console.log($scope.produk); ---> Object { category: "Sightseeing & Tours" }
            return $scope.produk;
        })
        .catch(function(response){

        })
    }

新表格模式:

在此处输入图片说明

编辑表单模式:

在此处输入图片说明

如何在编辑模式下将默认下拉列表替换为json返回的内容。 谢谢

除了使用下拉列表,还可以使用select-2指令来解决此问题。请参考以下链接: https : //github.com/angular-ui/ui-select2

暂无
暂无

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

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