码迷,mamicode.com
首页 > Web开发 > 详细

AngularJS 中ng-model通过$watch动态取值

时间:2017-06-21 20:05:05      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:charset   变化   javascrip   ppc   ng-model   bsp   https   长度   blog   

这个例子的意思是,当xxxx的长度不超过6时,xxxx和yyyy两个input的model是无关的,但当xxxx超过6,则yyyy会跟随其值而变化。

 

<!doctype HTML>
<html>

<head lang="en">
  <meta charset="utf-8">

  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  <script type="text/javascript">
    var app = angular.module(‘app‘, []);
    app.controller(‘AppCtrl‘, function($scope) {

      $scope.xxxx = "apple";
      $scope.yyyy = "banana";

      $scope.$watch(‘xxxx‘, function() {

        if ($scope.xxxx.length > 6) {
          $scope.yyyy = "***" + $scope.xxxx + "***";
        } else {}
      });
    });
  </script>


</head>

<body ng-app="app" ng-controller="AppCtrl">

  <input type="text" ng-model="xxxx">
  <input type="text" ng-model="yyyy">

  <br/>
  <p>{{xxxx}}</p>
  <p>{{yyyy}}</p>

</body>

</html>

  

AngularJS 中ng-model通过$watch动态取值

标签:charset   变化   javascrip   ppc   ng-model   bsp   https   长度   blog   

原文地址:http://www.cnblogs.com/pekkle/p/7061239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!