加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 具有Ajax功能的Angular-UI Select2的双向数据绑定

发布时间:2020-12-17 18:03:13 所属栏目:安全 来源:网络整理
导读:使用具有Ajax功能的AngularJS Angular-UI Select2的双向数据绑定. 我创建了一个指令,以通用的方式实现Select2下拉式Ajax行为: – (它需要很少的属性来获取formatResult,formatSelection方法来调用和url). 我的问题是如何在“编辑模式”中加载值,从格式选择
使用具有Ajax功能的AngularJS Angular-UI Select2的双向数据绑定.

我创建了一个指令,以通用的方式实现Select2下拉式Ajax行为: – (它需要很少的属性来获取formatResult,formatSelection方法来调用和url).

我的问题是如何在“编辑模式”中加载值,从格式选择方法接收下拉列表中的选定值,但在加载屏幕时,我想从绑定的相同值加载下拉列表.
例:-

<input type="hidden" for="part" class="bigdrop" style="width: 250px" formatresult="partFormatResult" formatselection="partFormatSelection" aurl="/api/Part" search-drop-down ui-select2="configPartSelect2" ng-model="product.SalesPart" data-placeholder="Select Part" ng-change="onPartSelect();" />

    Directive Code

    One23SRCApp.directive('searchDropDown',['$http',function (http) {
        return function (scope,elm,attrs) {
            var raw = elm[0];
            var fetchFuncName = "fetch" + attrs["uiSelect2"];
            console.log("Directive load pat  " + scope[attrs["ngModel"]]);
            scope[fetchFuncName] = function (queryParams) {
                var result = http.get(queryParams.url,queryParams.data).success(queryParams.success);
                result.abort = function () {
                    return null;
                };
                return result;
            };


            scope[attrs["uiSelect2"]] = {
                minimumInputLength: 3,initSelection: scope[attrs["initselection"]],ajax: {
                    url: attrs["aurl"],data: function (term,page) {
                            return { params: { isStockable: true,query: term,pageNo: page,pageSize: 20 } };
                    },dataType: 'json',quietMillis: 100,transport: scope[fetchFuncName],results: function (data,page) {
                        var more = (page * 20) <= data.length; // whether or not there are more results available
                        return { results: data,more: more };
                    }
                },formatResult: scope[attrs["formatresult"]],formatSelection: scope[attrs["formatselection"]],dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
            };

            return { bind: attrs["ngModel"] };
        };

    }]);

//inside controller-
after loading of data
   $("#partDD").select2("val",product.SalesPart);
//$scope.partInitSelection definition.
    $scope.partInitSelection = function (element,callback) {
        if (! $scope.PartList) {
            $scope.PartList = [$scope.product.SalesPart];
        } else {
            $scope.PartList.push($scope.product.SalesPart);
        }
        callback($scope.product.SalesPart);
    };

}

解决方法

最后解决了: –
由于我将配置对象保留在范围[attrs [“uiSelect2”]]中,
每次加载数据时,我都调用了上面配置对象的.ajax.data方法.

scope[attrs["uiSelect2"]].ajax.results(product.SalesPart.text,1);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读