<h1>Example: ngChange</h1>
<p>ngChangeはchangeイベントをリスナー登録します。ng-modelが定義されていないと動きません。</p>
<div ng-app="myApp">
  <div ng-controller="myController">
    <textarea type="text" ng-change="change()" ng-model="tweet"></textarea>
    <p>
      <span ng-hide="isOver">残り {{ count }}文字</span>
      <span ng-show="isOver">文字数オーバー</span>
    </p>
  </div>
</div>
@import "bourbon";

body {
  padding: 1.5rem;
}
View Compiled
var app = angular.module('myApp', []);

app.controller('myController', ['$scope', function($scope){
  var maxCount = 140;
  $scope.count = maxCount;
  
  $scope.change = function(){
    $scope.count = maxCount - $scope.tweet.length;
    
    if($scope.count < 0) {
      $scope.count = Math.abs($scope.count);
      $scope.isOver = true;
    } else {
      $scope.isOver = false;
    }
  }
}]);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js