angular添加类到span元素

时间:2015-03-31 07:08:24

标签: angularjs

我想使用angular

将class添加到span元素
<div class="input-group textbox-wrap fade-in two">                      
   <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>                         
   <input type="text" class="form-control" id="username" placeholder="{{'global.form.username.placeholder' | translate}}" 
      required="required" data-ng-model="username" ng-focus="focusInput($event)" ng-blur="blurInput($event)" autofocus />
</div>

我可以将类添加到父div,但我需要为span元素应用类。

在控制器中,

$scope.focusInput= function($event )
  {
    angular.element($event.target).parent().addClass('focused');    
  };

3 个答案:

答案 0 :(得分:1)

您可以在span元素上使用ngClass指令,基于您在焦点输入函数上设置的变量。

答案 1 :(得分:1)

你可以这样做:

<div class="input-group textbox-wrap fade-in two">                      
   <span class="input-group-addon" ng-class="{'yourClass': inputFocused}"><i class="glyphicon glyphicon-user"></i></span>                         
   <input type="text"  class="form-control" id="username" placeholder="{{'global.form.username.placeholder' | translate}}" 
      required="required" data-ng-model="username" ng-focus="focusInput()" ng-blur="blurInput($event)" autofocus />
</div>

$scope.focusInput= function($event )
  {
    $scope.inputFocused = true;
  };

答案 2 :(得分:1)

你可以在HTML本身上做到这一点。

<div class="input-group textbox-wrap fade-in two">
    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
      <input type="text" class="form-control" id="username" placeholder="{{'global.form.username.placeholder' | translate}}" 
       required="required"  ng-class="'focused':focused"
    data-ng-model="username" ng-focus="focused=true" ng-blur="focused=false" autofocus />
</div>