角度日志监视用户输入任何更改

时间:2017-08-08 20:36:41

标签: angularjs

app.controller("ListController", ['$scope', function($scope) {
$scope.personalDetails = [
                    {
                        'fname':'Muhammed',
                        'lname':'Shanid',
                        'email':'shanid@shanid.com',
                        'method':'66666',
                        'date':'08/08/20117',
                        //'specieSelected':'ALLIGATOR JUNIPER'
                        'specieSelected':{
                                            'originalObject': {'code':'','name':'ACACIA SPECIES'},
                                            'title':'ALLIGATOR JUNIPER'

                                            }

                    },

我有复杂的变量和多字段。 通过添加personalDetails来监视,每个用户输入都不会触发控制台日志。例如,用户更改fname,我想记录它。但事实并非如此。

$scope.$watch('personalDetails', function() {
                            console.log($scope.personalDetails);



                        }); 

1 个答案:

答案 0 :(得分:1)

deep watch level

   angular.module('MY_APP', []).controller('MyCtrl', MyCtrl)
   function MyCtrl($scope,$timeout) {
   $scope.users = [{"name": "vinoth"},{"name":"yusuf"},{"name":"rajini"}];

   $scope.$watch("users", function() {
      console.log("**** reference checkers $watch ****")
   });

   $scope.$watchCollection("users", function() {
    console.log("**** Collection  checkers $watchCollection ****")
   });

  $scope.$watch("users", function() {
      console.log("**** equality checkers with $watch(true) ****")
  }, true);

 $timeout(function(){
    console.log("Triggers All ")
    $scope.users = [];
    $scope.$digest();

     console.log("Triggers $watchCollection and $watch(true)")
    $scope.users.push({ name: 'Thalaivar'});
    $scope.$digest();

    console.log("Triggers $watch(true)")
     $scope.users[0].name = 'Superstar';
     $scope.$digest();
   });
 }

如果您想观看或控制每个用户输入的日志, 你应该使用相等的手表,它在第三位有真正的参数。

   $scope.$watch('personalDetails', function() {
                            console.log($scope.personalDetails);

                            }, true);