IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]ionic 中的 $scope.$watch 问题

    qcpm1983发表于 2015-08-18 15:33:05
    love 0

    angularjs 中的$watch 用来监控变量的变化,并做出改变.
    在 ionic 中也有这个需求,这有个小小的坑.

    <label class="item item-input">
         <i class="icon ion-search placeholder-icon"></i>
         <input type="text" placeholder="Search" ng-model="user">
         </label>
                {{user}}
                {{updated}}
      $scope.updated = -1;
      $scope.$watch('user.name', ()->
        console.log $scope.user
        $scope.updated++;
      , true
      );

    用 angular 这样的写法,死活不行.
    google 了下,需要把watch的内容放在一个父元素中.js 代码如下:

    
      $scope.user = {name: "sss"}
      $scope.updated = -1;
    
      $scope.$watch('user.name', ()->
        console.log $scope.user
        $scope.updated++;
      , true
      );
    

    html 代码

    <div class="list list-inset">
        <label class="item item-input">
            <i class="icon ion-search placeholder-icon"></i>
            <input type="text" placeholder="Search" ng-model="user.name">
        </label>
    {{user}}
    {{updated}}
    </div>

    本文参考
    http://forum.ionicframework.com/t/why-does-this-scope-watch-not-work/9430



沪ICP备19023445号-2号
友情链接