1. 程式人生 > >AngularJS 啟程三

AngularJS 啟程三

sed title 初始化 del scrip AR return 技術分享 row

技術分享圖片
<!DOCTYPE html>
<html lang="zh_CN">
 <head>
  <title>字數小例子</title>
 </head>
 <body ng-app="noCntAPP">
  <div ng-controller="noCntCtrl">
    <h2>我的筆記</h2>
    <textarea cols="30" rows="10" ng-model="txtArea"></textarea>
    
    <
p><button>讀取</button> <button>提交</button> <button>撤銷</button> <p>剩余字數: {{getCount()}}</p> </p> <div> <script type="text/javascript" src="./angular.js"></script> <script type="text/javascript"> angular.module(
noCntAPP,[]) .controller(noCntCtrl,[$scope,function($scope){ $scope.txtArea=‘‘; // 初始化文本區域值為空串 $scope.getCount=function(){ return 100 - $scope.txtArea.length; } }]); </script> </body> </
html>
剩余字數小例子

技術分享圖片
<!DOCTYPE html>
<html lang="zh_CN">
 <head>
  <title>字數小例子</title>
 </head>
 <body ng-app="noCntAPP">
  <div ng-controller="noCntCtrl">
    <h2>我的筆記</h2>
    <textarea cols="30" rows="10" ng-model="txtArea"></textarea>
    
    <p><button>讀取</button>
    <button>提交</button>
    <button>撤銷</button> 
    <p>剩余字數: {{getCount()}}</p>
    </p>
  <div>
  <script type="text/javascript" src="./angular.js"></script>
  <script type="text/javascript">
    angular.module(noCntAPP,[])
           .controller(noCntCtrl,[$scope,function($scope){
                $scope.txtArea=‘‘; // 初始化文本區域值為空串
                $scope.getCount=function(){
                   if($scope.txtArea.length>100){
                   $scope.txtArea = $scope.txtArea.slice(0,100); // 超過 100 截取前 0-99 個字符
                }
                   return 100 - $scope.txtArea.length;
                }
          }]);
  </script>
 </body>
</html>
改進一下加入字符串長度判斷

AngularJS 啟程三