ionic 中的 $scope.$watch 问题

来源:互联网 发布:linux系统简介 编辑:程序博客网 时间:2024/06/05 19:15

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

1 0
原创粉丝点击