2017-02-23 78 views
3

更改CSS样式AngularJS使用jQuery

var myApp = angular.module("myApp", []); 
 
myApp.controller("votesController", ['$scope', function($scope, $timeout) { 
 
    $scope.comments = [ 
 
    ]; 
 
    $scope.newComment = { 
 
    likes: 0 
 
    }; 
 
    $scope.createComment = function() { 
 
    if ($scope.newComment.comment != "") { 
 
     $scope.comments.push({ 
 
     comment: $scope.newComment.comment, 
 
     likes: $scope.newComment.likes 
 
     }); 
 
    } 
 
    }; 
 
    $scope.incrementLikes = function(comment) { 
 
    comment.likes++; 
 
    }; 
 
    $scope.decrementLikes = function(comment) { 
 
    comment.likes--; 
 
    }; 
 
}]); 
 

 
$('a.vote_comment').on('click',function(){ 
 
\t $(this).css('color','red'); \t \t \t \t \t \t 
 
\t }); 
 
$('a.vote_dis_like_comm').on('click',function(){ 
 
\t $(this).css('color','green'); 
 
\t });
a 
 
{ 
 
    cursor:pointer; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="container" ng-app="myApp"> 
 
    <div ng-controller="votesController"> 
 
    <div ng-repeat="comment in comments"> 
 
     <div class="comment_box_all"> 
 
     <div class="comment_user"> 
 
      <div class="comment_note"> 
 
      <a ng-click="incrementLikes(comment, $index)" class="vote_comment">Like</a> 
 
      <span class="num_vote_comm_11"> | {{comment.likes}} | </span> 
 
      <a ng-click="decrementLikes(comment, $index)" class="vote_dis_like_comm">Unlike</a> 
 
      </div> 
 
      <div class="content_text_user_ans"><span>{{comment.comment}}</span></div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    <div class="area_comm_tex"> 
 
     <textarea class="text_area" ng-model="newComment.comment" placeholder="Add comment"></textarea> 
 
     <button class="op_comm_now" ng-click="createComment()">Add text</button> 
 
    </div> 
 
    </div> 
 
</div>

这是非常简单的评论框,但我有一个问题。你能向我解释为什么用jQuery改变CSS样式不工作?我想要改变颜色文字/不像onclick,但这不起作用。

+0

你试图把它在的document.ready – chiliNUT

+2

为什么jQuery和不'ngClass' +'ngClick'?这是Angular,不要用jQuery代码污染它 –

+1

Angular对我来说是新的,我正在改变纯js和jquery中的代码,我认为它可以结合使用jquery和angular。但现在我会尝试以纯粹的角度做它 –

回答

3

你为什么不使用ng-style从控制器指定样式这样

$scope.incrementLikes = function(comment) { 
    comment.likes++; 
    $scope.likeColor = {'color': 'red'} 
    }; 
    $scope.decrementLikes = function(comment) { 
    comment.likes--; 
    $scope.dislikeColor = {'color': 'green'} 
    }; 

<a id="vote_comment" ng-click="incrementLikes(comment, $index)" class="vote_comment" ng-style="likeColor">Like</a> 
<span class="num_vote_comm_11"> | {{comment.likes}} | </span> 
<a ng-click="decrementLikes(comment, $index)" class="vote_dis_like_comm" ng-style="dislikeColor">Unlike</a> 
</div> 

var myApp = angular.module("myApp", []); 
 
myApp.controller("votesController", ['$scope', function($scope, $timeout) { 
 
    $scope.comments = [ 
 
    ]; 
 
    $scope.newComment = { 
 
    likes: 0 
 
    }; 
 
    $scope.createComment = function() { 
 
    if ($scope.newComment.comment != "") { 
 
     $scope.comments.push({ 
 
     comment: $scope.newComment.comment, 
 
     likes: $scope.newComment.likes, 
 
     likeColor : {}, 
 
     dislikeColor : {} 
 
     }); 
 
    } 
 
    }; 
 
    $scope.incrementLikes = function(comment) { 
 
    comment.likes++; 
 
    comment.likeColor = {'color': 'red'} 
 
    }; 
 
    $scope.decrementLikes = function(comment) { 
 
    comment.likes--; 
 
    comment.dislikeColor = {'color': 'green'} 
 
    }; 
 
}]);
a 
 
{ 
 
    cursor:pointer; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="container" ng-app="myApp"> 
 
    <div ng-controller="votesController"> 
 
    <div ng-repeat="comment in comments"> 
 
     <div class="comment_box_all"> 
 
     <div class="comment_user"> 
 
      <div class="comment_note"> 
 
      <a id="vote_comment" ng-click="incrementLikes(comment, $index)" class="vote_comment" ng-style="comment.likeColor">Like</a> 
 
      <span class="num_vote_comm_11"> | {{comment.likes}} | </span> 
 
      <a ng-click="decrementLikes(comment, $index)" class="vote_dis_like_comm" ng-style="comment.dislikeColor">Unlike</a> 
 
      </div> 
 
      <div class="content_text_user_ans"><span>{{comment.comment}}</span></div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    <div class="area_comm_tex"> 
 
     <textarea class="text_area" ng-model="newComment.comment" placeholder="Add comment"></textarea> 
 
     <button class="op_comm_now" ng-click="createComment()">Add text</button> 
 
    </div> 
 
    </div> 
 
</div>

+0

谢谢,这对我来说是一个很大的经验! –

+0

当然没问题:D –

+0

你可以看看最后一个问题吗?如果你添加了例如3评论,并尝试点击喜欢或不喜欢所有改变颜色。如何让只有点击/不喜欢改变颜色?就像$(this)在jquery –

1

实际上,你可以做到这一点with CSS

a.vote_comment:active { 
    color: red; 
} 
a.vote_dis_like_comm:active { 
    color: green; 
} 

您可以设置为链接的方方面面颜色:

/* unvisited link */ 
a:link { 
    color: green; 
} 

/* visited link */ 
a:visited { 
    color: green; 
} 

/* mouse over link */ 
a:hover { 
    color: red; 
} 

/* selected link */ 
a:active { 
    color: yellow; 
}