2016-05-01 87 views
1

嗨我试图更好地了解angularJs手表功能 为了做到这一点,我做了以下bin enter link description here 其中包含一块手表。 手表第一次正确执行,我可以看到新旧价值。 我第二次为这两个数字和字符串输入框我DONOT看到新旧值进入登录

$scope.$watch(function(theScope){ 

console.log(typeof theScope.price ==='number'); //logs true 
     if(typeof theScope.price ==='number'){ 

     return theScope; 
     }else{ 
     return false 
     } 

    },function(oldValue,newValue){ 
     console.log('old:'+oldValue); 
     console.log('new:'+newValue); 
    }); 

回答

1

有几件事情。如果你想使用函数的语法在你的手表,写这样的:

var app = angular.module('app',[]); 
app.controller('MainController',['$scope', 
    function($scope){ 
    $scope.message =20; 
    $scope.$watch(function($scope){ 
console.log(typeof $scope.message ==='number'); 
     return $scope.message; 
    },function(oldValue,newValue){ 
     console.log('old:'+oldValue); 
     console.log('new:'+newValue); 
    }); 
    } 
]); 

而且,你在你的HTML结合message,但你在你的手表用price。另外,您正在使用的theScope,实际上并没有用于别名$scope

将该代码粘贴到您的垃圾箱,它应该工作。

0

可以$watch价格只是

$scope.$watch('price',function(oldValue,newValue){ 
    if(typeof newValue =='number'){ 
     console.log('old:'+oldValue); 
     console.log('new:'+newValue); 
    } 
}); 

也改变ng-model='message'ng-model='price'

fiddle