Display AngularStrap Dropdown Manually - How?

萝らか妹 提交于 2019-12-04 23:19:47

For anyone interested, after digging through the source code, I discovered an attribute on directive bsDropdown called bsShow with the following implementation.

// Visibility binding support
attr.bsShow && scope.$watch(attr.bsShow, function(newValue, oldValue) {
    if(!dropdown || !angular.isDefined(newValue)) return;
    if(angular.isString(newValue)) newValue = !!newValue.match(/true|,?(dropdown),?/i);
    newValue === true ? dropdown.show() : dropdown.hide();
});

This essentially drives the dropdown markup to include this and bind to a $scope value as such

<textarea id="textdrop" ng-model="expression" intellisense bs-dropdown="dropdown" bs-show="drop"></textarea>

Then within my directive I can trigger visibility by modifying $scope.drop as bound on bs-show="drop"

app.directive('intellisense', [function () {
    return {
        restrict: 'A',
        link: function (scope, elem, attrs) {
          scope.$watch(attrs.ngModel, function (v) {
                if(v) {
                  scope.drop = true; // simple - but works
                } else {
                  scope.drop = false;
                }
            });
        }
    }
}]);

It appears this was documented on a project commit as referenced here. The official documentation still makes no mention of this as the time of writing, and given the timeline of this I am surprised the lack of attention this has received.

Plunker Link with trigger: manual

When I have a dropdown as above and want to trigger it manually, I add an id to the element :

<button id="myDropdown" type="button" class="btn btn-lg btn-primary" data-animation="am-flip-x" bs-dropdown="dropdown">
    Click to toggle dropdown
</button>

and then simply trigger the elements click() method :

$scope.dropdown = angular.element("#myDropdown");
...
$scope.dropdown.click();

demo -> http://plnkr.co/edit/v5AuBOiMN6TZCPE4eYk2?p=preview

This can be combined with angular-hotkeys :

hotkeys.bindTo($scope)
    .add({
        combo: '<key-combination>',
        description: '<description>',
        callback: function() {
            $scope.dropdown.click()
        }
    })

Manually triggering datepickers (or any dropdown) is now much easier with v2.0.4 of ngStrap. For more details, see this Github comment.

Here's a working plunk to demonstrate the datepicker as both a manually-triggered dropdown, and as a manually-triggered inline element.

<input type="text" ng-model="dropdownDatepicker.date" bs-datepicker data-trigger="manual" data-bs-show="dropdownDatepicker.show">

(And you really don't even need the data-trigger="manual", so you can leave that off if you want).

As for the intellisense directive, that doesn't sound like the issue here, so I'll leave that to you...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!