Angular.js 1.4.2 CSS动画不适用于ng-show

时间:2015-07-10 22:09:38

标签: javascript css angularjs css-animations

我正在尝试添加CSS动画来点击按钮,因此数据要么淡入还是没有。但是,当前单击按钮会立即将类更改为ng-hide和从ng-hide更改类,因此动画永远不会被调用。我正在努力根据Angular.js网站上提供的示例进行工作:https://docs.angularjs.org/api/ng/directive/ngShow

网站上提供的工作代码的插件位于: http://plnkr.co/edit/2ZJ9pdUq1tXLbDLuShEV?p=preview

我的代码,由于某种原因没有任何动画,在这里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"}];
  });
})();

在我的代码中,由于某种原因,该元素似乎永远不会获得ng-hide-add或ng-hide-remove类。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您需要将ngAnimate注入您应用的模块:

(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"}];
  });
})();