Javascript Angularjs从另一个输入框更新输入
嗨,我有以下情况 我有以下几个问题Javascript Angularjs从另一个输入框更新输入,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,嗨,我有以下情况 我有以下几个问题 <body ng-app="myApp" ng-controller="MyController"> <input type="text" placeholder="enter a number" ng-model="c.num" on-blur="test()" /> <input type="text" ng-model="c.Id" ng-init="c.Id = pId" /> </
<body ng-app="myApp" ng-controller="MyController">
<input type="text" placeholder="enter a number" ng-model="c.num" on-blur="test()" />
<input type="text" ng-model="c.Id" ng-init="c.Id = pId" />
</body>
因此,当在第一个框中输入5时,第二个框应更新6。但事实并非如此。
请让我知道如何修理它。谢谢
这是它的好处
ng模糊是一种方法: 标记:
<body ng-app="myApp" ng-controller="MyController">
<input type="text" placeholder="enter value" ng-model="num" ng-blur="test()" />
<input type="text" ng-model="pId" />
</body>
请检查第一个ng型号是否正确。我想你想要这个:
<body ng-app="myApp" ng-controller="MyController">
<input type="text" placeholder="enter a number" ng-model="num" on-blur="test()" />
<input type="text" ng-model="c.Id" ng-init="c.Id = pId" />
</body>
通过查看您的代码,我不确定对象“c”是什么或在哪里实例化。我也不认为您正确地使用了ng init。在下面的文档中,建议仅在角度上下文中不可用的范围内使用它(例如ng重复) 将html更改为
<body ng-app="myApp" ng-controller="MyController">
<input type="text" placeholder="enter a number" ng-model="num" ng-blur="test()" />
<input type="text" ng-model="pId" />
</body>
保持javascript不变
编辑:
角度文档工作
- 我删除了
,不需要它李>ng init
- 引用了正确的变量
- 将输入类型更改为number(然后无需解析int)。否则你会得到5+1=51
$scope.test=function() { $scope.c.Id = $scope.c.num + 1; };
你的我想你忘了粘贴plunker链接。是的,我已经粘贴了,很抱歉。它是加在上面的。谢谢!我刚刚在plunker中将我的html粘贴到你的html上,它似乎可以按照你的要求工作。对你有用吗?我更新了代码以显示更多信息。。。在控制器中调用
test
的第一个输入框中,使用ng blur捕捉焦点丢失的时间,并使用该方法将值设置为pId
<body ng-app="myApp" ng-controller="MyController">
<input type="text" placeholder="enter a number" ng-model="num" ng-blur="test()" />
<input type="text" ng-model="pId" />
</body>
$scope.test=function() {
$scope.c.Id = $scope.c.num + 1;
};