我正在使用angularJS创建一个项目。我在项目中使用手表时遇到问题。我正在更新数组的内部对象。

 $scope.$watch('demoData',function(_n, _o) {
      console.log("watchExecuting")
      },true);


这是Jsfiddle:
http://jsfiddle.net/HB7LU/29132/

最佳答案

您需要使用ng-model值监视输入,如下所示



var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

	$scope.demoData = []
  $scope.lid = "f0b751df4f0444d8";
  $scope.demoData[$scope.lid] = {"textBox":"test"}
 	console.log( $scope.demoData)
  $scope.demo = function(){
   //console.log( $scope.demoData)
  }
  $scope.$watch('demoData[lid].textBox',function(_n, _o) {
  console.log("watchExecuting")
  },true);
});

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
 <input type = "text" ng-model="demoData[lid].textBox" ng-change="demo()">
  </body>

</html>

关于javascript - 更新数组中的内部对象时 watch 未执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43471051/

10-11 13:41