简体   繁体   中英

Angular.js 1.4.2 CSS Animations not working for ng-show

I am trying to add a CSS animation to clicking on the buttons, so the data either fades in or not. But currently clicking the buttons just instantly changes the class to and from ng-hide, so the animations never get called. I am trying to work based on the examples given on the Angular.js website here: https://docs.angularjs.org/api/ng/directive/ngShow

The plunker for the working code given on the website is here: http://plnkr.co/edit/2ZJ9pdUq1tXLbDLuShEV?p=preview

My code, which does not animate whatsoever for some reason, is here : http://plnkr.co/edit/vnvVUciM5qMVyqzv2nL0?p=preview

HTML:

  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

  <!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

  <script src="http://code.angularjs.org/1.4.2/angular.js"></script>
  <script src="http://code.angularjs.org/1.4.2/angular-animate.min.js"></script>
  <script src="script.js"></script>

</head>
<body ng-app="assignment" ng-controller="AssignmentController as assignment">
  <button type="button" ng-click="current_question_id = 0">0</button>
  <button type="button" ng-click="current_question_id = 1">1</button>

  <div class="panel-body" id="form_content">
    <div ng-repeat="question in questions" ng-show="question.id == current_question_id" class="question-container">
      <div class="position-center">
        <div class="form-horizontal">
          <div class="form-group">
            <label for="question_question_{{ question.id }}" class="col-lg-2 col-sm-2 control-label">Question</label>
            <div class="col-lg-10">
              <input type="text" id="question_question_{{ question.id }}" name="questions[][question]" placeholder="Question" class="form-control" ng-value="question.question">
              <p class="help-block">{{ question.type }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

CSS:

.question-container {
  line-height: 20px;
  opacity: 1;
  padding: 10px;
}

.question-container.ng-hide-add.ng-hide-add-active,
.question-container:not(.ng-hide) {
  -o-transition: all linear 0.5s;
  -moz-transition: all linear 0.5s;
  -webkit-transition: all linear 0.5s;
  transition: all linear 0.5s;
}

.question-container.ng-hide {
  line-height: 0;
  opacity: 0;
  padding: 0 10px;
}

JS:

(function(){
  var app = angular.module('assignment', []);
  app.controller('AssignmentController', function($scope, $http){
    $scope.questions = [{id: 0, question: "What is the capital of Greenland?", type: "Short Answer"},
    {id: 1, question: "Wtf is going on?", type: "Long Answer"}];
  });
})();

It seems like in my code for some reason the element never gets the ng-hide-add or ng-hide-remove classes. Any ideas? Thanks!

You need to inject ngAnimate into your app's module:

(function(){
  var app = angular.module('assignment', ['ngAnimate']);
  app.controller('AssignmentController', function($scope, $http){
    $scope.questions = [{id: 0, question: "What is the capital of Greenland?", type: "Short Answer"},
    {id: 1, question: "Wtf is going on?", type: "Long Answer"}];
  });
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM