【问题标题】:Angular + Bootstrap 2.3 - Dynamic tooltipAngular + Bootstrap 2.3 - 动态工具提示
【发布时间】:2014-07-01 07:40:55
【问题描述】:

我正在尝试在基于 Angular + Bootstrap 2.3 的应用程序中实现动态工具提示显示。由于系统限制,我不能使用 Angular-UI。

要求是根据错误情况显示自定义工具提示。 例如,如果数据与预期模式不匹配,则将“无效数据”显示为工具提示。但是如果输入的数据是正确的,它应该显示默认的工具提示。 同样基于其他错误场景,例如超过最大长度等,将显示特定的错误消息。

我试图通过监听 Angular 添加到元素的错误类来在指令中实现这些。下面是代码:

TestApp.directive('dynamicTooltip', function() {

        var link = function (scope, element, attrs) {

            // Set dt-original-title attribute value to HTML Title attribute value
            if (angular.isDefined(attrs.title)){
                element.attr('dt-original-title', attrs.title); 
            }

            // Override dt-original-title attribute value to dt-title attribute value (if set)
            if (angular.isDefined(attrs.dtTitle)){
                element.attr('dt-original-title', attrs.dtTitle);   
            }  

            scope.$watch(function() {
                return element.attr('class');
            }, function (newValue) {

                var classes = newValue;

                if (element.hasClass("ng-invalid-required") && angular.isDefined(attrs.dtRequired)) {
                    $(element).attr('title', attrs.dtRequired);
                } else if (element.hasClass("ng-invalid-minlength") && angular.isDefined(attrs.dtMinlength)) {
                    $(element).attr('title', attrs.dtMinlength);
                } else if (element.hasClass("ng-invalid-min") && angular.isDefined(attrs.dtMin)) {
                    $(element).attr('title', attrs.dtMin);
                } else if (element.hasClass("ng-invalid-maxlength") && angular.isDefined(attrs.dtMaxlength)) {
                    $(element).attr('title', attrs.dtMaxlength);
                } else if (element.hasClass("ng-invalid-max") && angular.isDefined(attrs.dtMax)) {
                    $(element).attr('title', attrs.dtMax);
                } else if (element.hasClass("ng-invalid-pattern") && angular.isDefined(attrs.dtPattern)) {
                    $(element).attr('title', attrs.dtPattern);
                } else {
                    if (angular.isDefined(element.attr("dt-original-title"))) {     //Reset to original tooltip
                        $(element).attr('title', element.attr("dt-original-title"));                            
                    } else {
                        $(element).removeAttr("title");     // Remove if title is not set.
                    }
                }
            });
        }

        return {
            restrict: 'A',
            link: link
        }
    });

HTML:

<input id="txt1" type='text' 
    title='Default textbox tooltip'         
    ng-minlength="1"
    ng-maxlength='3'
    ng-model="myText1"
    ng-pattern="/^[a-zA-Z]+$/"          
    dynamic-tooltip
    dt-title="My customized tooltip"
    dt-maxlength="Maximum length exceeded"
    dt-minlength="Minimum length required"
    dt-required="This field is required"
    dt-pattern="Invalid data"
    required />

我想知道这是正确的方法还是有更好的选择。

【问题讨论】:

    标签: angularjs tooltip twitter-bootstrap-2


    【解决方案1】:

    我相信此解决方案可能适合您: http://plnkr.co/edit/jLThSTrLUapAG8OLW44m?p=preview

    这是我对类似工具提示相关问题的回答: Angular UI Tooltip overflowing screen

    请注意,我们必须扩展上述解决方案,因为在这种情况下,工具提示必须是动态的。我们可以在这里使用双花括号:

    <div tooltip="{{dynamicTooltip}}" placement="right">Some content</div>
    

    在 Angular 代码中,我们会这样做:

    $scope.dynamicTooltip = "Default Message";
    $scope.someFunction = function() {
       //fetch data or perform some process
       $http.get('some/url')
         .success(function (data) {
           $scope.dataProperty = data.someProperty;
           $scope.dynamicTooltip = $scope.dataProperty;
         })
         .error( fuction(data, status, headers, config) {
           $scope.dynamicTooltip = "Error Message!";  //or = status, if != undef
         });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多