gpt4 book ai didi

javascript - 即使 ng-model 存在于 AngularJS 中,ng-change 也不起作用

转载 作者:行者123 更新时间:2023-12-03 03:51:15 25 4
gpt4 key购买 nike

大家好,我已经在线检查了我的问题的解决方案,但没有解决我的问题。

HTML5 代码:

<!DOCTYPE html>
<html>
<head>
</head>
<body ng-app="myApp">
<div id="div1" ng-controller = "myCont">
<input id="txt1" type="text" ng-model="a" ng-change="updateC()">
<input id="txt2" type="text" ng-model="b">
<div>a {{a}}</div>
<div>s {{s}}</div>
<div>c {{c}}</div>
<script src="angular.js"></script>
<script src="angular_try.js"></script>
</body>
</html>

下面是我的 angularjs 代码:

var myApp= angular.module("myApp",[])

myApp.controller("myCont",["$scope","$rootScope",function($scope,$rootScope)
{
$scope.s=0;
$scope.a =0;
$scope.b=0;
function updateC()
{
$scope.c = $scope.a+20;
}


$scope.$watch("c",function(newvalue,oldvalue){
if(newvalue!=oldvalue)
{
$scope.c =$scope.c
}
})
}])

有人可以让我知道我的代码有什么问题吗,因为我将 ng-change 与 ng-model 一起使用,并且也有相同的监视表达式,但代码无法运行,但没有错误。

最佳答案

就这么做

$scope.updateC = function ({
$scope.c = $scope.a+20;
}

关于javascript - 即使 ng-model 存在于 AngularJS 中,ng-change 也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45149459/

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