您可以执行此
<div class="form-group {{focusIsSet ? 'is-focused': ''}}">
<label>Name</label>
<input type="text" name="name" class="form-control" ng-model="abc.user.name" ng-focus="focusIsSet = true" ng-blur="focusIsSet = false" required>
</div>
凡$scope.focusIsSet
布尔属性。所以,要看它的状态,你可以在<div class="form-group">
与表达方式管理课程{{focusIsSet ? 'is-focused': ''}}
你跟ng-focus
和ng-blur
指令
UPDATE改变它
我认为你可以追究国家与方式,每个输入
<div class="form-group {{checkFocusState('abc.user.name') ? 'is-focused': ''}}">
<label>Name</label>
<input type="text" name="name" class="form-control" ng-model="abc.user.name" ng-focus="setFocus('abc.user.name')" ng-blur="setBlur('abc.user.name')" required>
</div>
</div>
JS代码
var inputsFocusState = {};
$scope.checkFocusState = function(propertyPathName) {
if(inputsFocusState[propertyPathName] == true) {
return true;
}
return false
}
$scope.setBlur = function(propertyPathName) {
inputsFocusState[propertyPathName] = false;
}
$scope.setFocus = function(propertyPathName) {
inputsFocusState[propertyPathName] = true;
}
否则,您可以创建每个focus属性为每个输入HTML模板
附: ng-class
也是不错的选择
P.S.S我有类似的情况,但形式是完全动态的。
因此,我将每个属性拆分为对象user.name = {value: 'john', buttons: [...], label: 'Name', //and much more}
。
还更好地将'user.name.path'
更改为类似'user-name-path'
。
谢谢。你的代码中有一个小错误:它应该是'elem.toggleClass(attrs.focusClass);'你说得对,我确实需要两个监听器。但一个简单优雅的解决方案 – Steve
oops on class name ...更新并且添加了2位听众 – charlietfl
太好了。我使用'addClass'和'removeClass'而不是'toggleClass',但我想这两种方法都有效。这是一个很好的解决方案,再次感谢。 – Steve