【问题标题】:AngularJS+Bootstrap-UI: Enable tooltip on button when button is disabledAngularJS + Bootstrap-UI:禁用按钮时在按钮上启用工具提示
【发布时间】:2015-03-25 07:12:34
【问题描述】:

请在此处查看我的 jsfiddle 代码 http://jsfiddle.net/695qtssv/2/

如何让按钮在禁用时显示工具提示?

html

<div class="panel panel-default">
            <div  ng-repeat="item in itemDetails" tooltip="{{item.name + (isDisabled(item.name)?' is not available' : '')}}">
              <button ng-disabled="isDisabled(item.name)" class="btn btn-primary" ng-click="select(item)">{{item.name}}</button>
            </div>
        </div>

JS:

var myApp = angular.module('myApp', ['ui.bootstrap']);

function MyCtrl($scope) {
    $scope.myModel = "Tooltip only works when input is enabled.";
    $scope.isDisabled = false;

}

我已尝试在包装按钮的 div 上使用工具提示,但仍然没有运气,如示例中所示。

此工具提示适用,但我无法在我正在开发的应用程序中使用它。

任何帮助将不胜感激。

【问题讨论】:

    标签: angularjs angular-ui-bootstrap


    【解决方案1】:

    我认为禁用的元素不会触发鼠标事件。 见Event on a disabled input

    基于上面的链接,我提供了这种解决方案:

    <div class="panel panel-default">
      <div  ng-repeat="item in itemDetails" style="display:inline-block; position:relative;">
        <button ng-disabled="isDisabled(item.name)" class="btn btn-primary" ng-click="select(item)">{{item.name}}</button>
        <div style="position:absolute; left:0; right:0; top:0; bottom:0;" tooltip="{{item.name + (isDisabled(item.name)?' is not available' : '')}}"></div>
      </div>
    </div>
    

    小提琴:http://jsfiddle.net/695qtssv/3/

    【讨论】:

    • 正如当前 jsFiddle 中所展示的,工具提示行为符合预期,但按钮呈现为不可点击(即使启用)
    • 将覆盖的 div 放在禁用标志上的 ng-show 中并设置样式以显示“不允许”光标完成错觉并恢复功能。
    【解决方案2】:

    基本上不能直接设置输入元素的tooltip,禁用时显示。这是因为在禁用输入时浏览器不会触发任何事件。这在issue 中有描述。

    但是,您走在正确的道路上。您必须包装输入元素。我从上面的问题中得到了这个解决方案。

    var myApp = angular.module('myApp', ['ui.bootstrap']);
    
    function MyCtrl($scope) {
        $scope.myModel = "Tooltip only works when input is enabled.";
        $scope.isDisabled = false;
        
    }
    .layer-mask {
         position: relative;
    }
    .layer {
        position: absolute;
        top: 0;
        left:0;
        right:0;
        bottom:0;
    }
    .layer-mask button[disabled] {
        position: relative;
        z-index: -1;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
    <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" />
    <br />
    <br />
    <br />
    <div ng-app="myApp" ng-controller="MyCtrl">
        <div class="container">
            <div class="row">
                <div class="layer-mask" tooltip="My Tooltip">
                    <div class="layer"></div>
                    <button class="input-xxlarge" ng-disabled="isDisabled" ng-model="myModel">
                        My disabled button
                    </button>
                </div>
            <br/>
            Disable/Enable <input type="checkbox" ng-model="isDisabled"/>
    
        </div>
        </div>

    【讨论】:

    • 这不是一个完全足够的解决方案。工具提示可以正常工作,但点击事件在启用的按钮上不起作用。它们被“层”div 阻止。
    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 2019-07-08
    • 2017-03-02
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    相关资源
    最近更新 更多