【发布时间】:2014-03-28 15:46:03
【问题描述】:
我正在使用 AngularJS 和 Twitter Bootstrap (TB),我使用 Angular 的验证工具和 TB 的一些好东西,例如工具提示。这就是 HTML 的呈现方式:
<input
type="text"
id="company_taxId"
ng-model="company.taxId"
required="required"
class="input ng-scope ng-pristine ng-valid-maxlength ng-valid-minlength ng-valid-pattern ng-invalid ng-invalid-required"
style="width:485px;"
ng-minlength="9"
maxlength="9"
ng-maxlength="9"
ng-pattern="/^[\d{9}$]/"
placeholder="Ej: 458965879"
tooltip=""
wv-def=""
tooltip-trigger="focus"
tooltip-placement="right"
wv-cur=""
wv-err="Este valor debería ser un número válido."
wv-req="Este campo es requerido"
wv-min="Este valor debería tener exactamente 9 caracteres"
wv-max="Este valor debería tener exactamente 9 caracteres"
>
这就是我的指令在 Angular 方面的看法:
app.directive('validated', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.keyup(function(){
parent = $('.tooltip');
target = parent.find('.tooltip-inner');
if( element.hasClass('ng-invalid-required') ){
target.html(attrs.wvReq);
attrs.wvCur = attrs.wvReq;
parent.addClass('error');
}
else if( element.hasClass('ng-invalid-email') ){
target.html(attrs.wvEml);
attrs.wvCur = attrs.wvEml;
parent.addClass('error');
}
else if( element.hasClass('ng-invalid-minlength') ){
target.html(attrs.wvMin);
attrs.wvCur = attrs.wvMin;
parent.addClass('error');
}
else if( element.hasClass('ng-invalid-maxlength') ){
target.html(attrs.wvMax);
attrs.wvCur = attrs.wvMax;
parent.addClass('error');
}
else if( element.hasClass('ng-invalid') ){
target.html(attrs.wvErr);
attrs.wvCur = attrs.wvErr;
parent.addClass('error');
}
else{
target.html(attrs.wvDef);
attrs.wvCur = attrs.wvDef;
parent.removeClass('error');
}
});
element.focus(function(){
attrs.tooltip = attrs.wvCur;
setTimeout(function(){
parent = element.next('.tooltip');
target = parent.find('.tooltip-inner');
target.html( ( attrs.wvCur != "" ? attrs.wvCur : attrs.wvDef ) );
console.log()
if( attrs.wvCur != attrs.wvDef && attrs.wvCur != "" )
parent.addClass('error');
},5);
});
}
};
});
您可能注意到tooltip 是空的,但我用它来触发其他消息。如果tooltip 为空,则不显示其他消息,是否存在任何方法来触发带有空工具提示值的 TB 工具提示?
【问题讨论】:
-
尝试使用
"&nbsp;"而不是空字符串""。 -
@SparoHawk nop,没用
标签: javascript jquery angularjs twitter-bootstrap twitter-bootstrap-tooltip