gpt4 book ai didi

angularjs - Angular $watch 不工作?

转载 作者:行者123 更新时间:2023-12-05 01:28:36 26 4
gpt4 key购买 nike

下面的代码仅在页面最初加载时记录。怎么了?

<!DOCTYPE html>
<html ng-app>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
</head>
<body>
<div ng-controller="PageController">
<p ng-bind="model.header"></p>
<p>Value: <input ng-model="model.header" /></p>
</div>
<script>
function PageController($scope) {
$scope.model = {};
$scope.model.header = 'Header';

$scope.onModelChange = function (newValue, oldValue, scope) {
console.log('model changed > newValue: ' + newValue + ' > oldValue: ' + oldValue);
};

$scope.$watch($scope.model, $scope.onModelChange, true);
}
</script>
</body>
</html>

最佳答案

$watch 的第一个参数应该是范围属性本身

$scope.$watch("model", $scope.onModelChange);

而且您不需要对此属性启用深度监视(第三个参数)

关于angularjs - Angular $watch 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com