2017-04-18 17 views
1

我正在使用angularJS創建項目。在我的項目中使用手錶時遇到問題。我正在更新數組的內部對象。在更新數組中的內部對象時不執行監視

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

這裏是的jsfiddle: http://jsfiddle.net/HB7LU/29132/

+1

任何具體原因\t $ scope.demoData = []將其定義爲數組,因爲如果它定義爲它的對象'' $ scope.demoData = {}'http://jsfiddle.net/HB7LU/29134/ –

回答

2

你需要監視與NG-模型值的輸入像下面

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

 
app.controller('MainCtrl', function($scope) { 
 
    
 
\t $scope.demoData = [] 
 
    $scope.lid = "f0b751df4f0444d8"; 
 
    $scope.demoData[$scope.lid] = {"textBox":"test"} 
 
    \t 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>

相關問題