简体   繁体   English

AngularJS - 在ng-options完成填充选择后,jquery插件SelectBoxIt

[英]AngularJS - jquery plugin SelectBoxIt after ng-options finish populating select

I'm populating a select box using AngularJS with the following code: 我正在使用AngularJS填充选择框,其中包含以下代码:

<select ng-model="department" 
        ng-options="dept as dept.name for dept in departmentList" 
        class="fancy">
    <option value="">-- select an option --</option>
</select>

But I need to render this SELECT box using the SelectBoxIt jquery plugin. 但是我需要使用SelectBoxIt jquery插件渲染这个SELECT框。

A hard-coded version of the SELECT works just fine as I have the initialization of SelectBoxIt at the bottom of the controller handling this section of the page: SELECT的硬编码版本工作正常,因为我在控制器底部的SelectBoxIt初始化处理页面的这一部分:

$targetSelects.selectBoxIt({
            theme: "bootstrap"
            , downArrowIcon: "arrow"
            , showFirstOption: false
        });
        $targetSelects.on({
            "open" : onOpen
            , "close" : onClose
        });

I am now thinking that SelectBoxIt initialization is happening before the angular-populated SELECT box is finished populating. 我现在认为SelectBoxIt初始化发生在角度填充的SELECT框完成填充之前。

Does this seem likely? 这看起来有可能吗? If so, how might I solve this? 如果是这样,我该如何解决这个问题呢? I'm thinking this is probably a case of using a Deferred object, but am not sure where to inject this. 我认为这可能是使用Deferred对象的情况,但我不知道在哪里注入它。

Any help would be appreciated. 任何帮助,将不胜感激。

Update 更新

I re-implemented the plugin call as a directive. 我重新实现了插件调用作为指令。 It does work. 它确实有效。 It initiates the drop-down as a SelectBoxIt select box, but it is still being done BEFORE angular has populated the dropdown using ng-options.... 它启动下拉菜单作为SelectBoxIt选择框,但它仍然在角度已使用ng-options填充下拉列表之前完成....

.directive('fancySelect', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {

            var $targetSelects = $(element),
                selectConfig = {
                    theme: "bootstrap",
                    downArrowIcon: "arrow",
                    showFirstOption: false
                };

            function onOpen(event){
                $(this).data("selectBox-selectBoxIt").downArrow.addClass("drop-up");
                log('onOpen');
            }

            function onClose(event){
                $(this).data("selectBox-selectBoxIt").downArrow.removeClass("drop-up");
                log('onClose');
            }

            $targetSelects.selectBoxIt(selectConfig);
            $targetSelects.on({
                "open" : onOpen,
                "close" : onClose
            });
            $targetSelects.selectBoxIt('refresh');
        }
    };
});

If you weren't using Angular.js, you could use the populate option to add options to your SelectBoxIt dropdown: http://gregfranko.com/jquery.selectBoxIt.js/#PopulateOptions 如果您没有使用Angular.js,可以使用populate选项向SelectBoxIt下拉列表中添加选项: http ://gregfranko.com/jquery.selectBoxIt.js/#PopulateOptions

Since you are using Angular, if there is some event that you can listen to once your select box is populated, then you could call the SelectBoxIt refresh() method to update your dropdown, like this: 由于您使用的是Angular,如果在填充选择框后有一些事件可以收听,那么您可以调用SelectBoxIt refresh()方法来更新您的下拉列表,如下所示:

    // Once your original select box is populated
    $('select').selectBoxIt('refresh');

One way of doing it is to watch the model and then fire an event on the next digest cycle. 一种方法是观察模型,然后在下一个摘要周期中触发事件。

scope.$watch(model, function() {
    scope.$evalAsync(function() {
        // event
    });
});

I've written this up properly here: Detect when ng-options has finished rendering . 我在这里正确地写了这个: 检测ng-options何时完成渲染

I've solved part of the problem here, which may work for some, using $timeout in the directive. 我在这里解决了部分问题,这可能对某些人有用,在指令中使用$ timeout。 Whilst this, for me, solves the problem of the options being rendered, unfortunately, at the time of writing this, I can't get them to function! 虽然这对我来说解决了渲染选项的问题,但不幸的是,在撰写本文时,我无法让它们发挥作用!

.directive('fancySelect', function($timeout) {
return {
    restrict: 'A',
    link: function(scope, element, attrs) {
     $timeout(function() {
        var $targetSelects = $(element),
            selectConfig = {
                theme: "bootstrap",
                downArrowIcon: "arrow",
                showFirstOption: false
            };

        function onOpen(event){
            $(this).data("selectBox-selectBoxIt").downArrow.addClass("drop-up");
            log('onOpen');
        }

        function onClose(event){
            $(this).data("selectBox-selectBoxIt").downArrow.removeClass("drop-up");
            log('onClose');
        }

        $targetSelects.selectBoxIt(selectConfig);
        $targetSelects.on({
            "open" : onOpen,
            "close" : onClose
        });
        $targetSelects.selectBoxIt('refresh');
    }
  }, 0);
};

}); });

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

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