如何设置从其他两个数字字段计算出的angularjs模型值

编程入门 行业动态 更新时间:2024-10-24 06:36:38
本文介绍了如何设置从其他两个数字字段计算出的angularjs模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我目前正在开发一个AngularJS ionic应用程序,但是在视图中设置模型值时遇到问题.这是定义字段的简单形式.

Hi I am currently working on one AngularJS ionic app but facing a problem in settings up model value in the view. It a simple form with fields defined.

<input type="number" ng-model="form.field1" /> <input type="number" ng-model="form.field2" ng-change="count=form.field1+form.field2" /> <input type="number" ng-model="form.field3" ng-init="form.field3=count" ng-value="count"/>

在第三个字段上,我可以看到显示的值,但是当提交到服务器时,它的值为"0"而不是计数值.

On third field I can see the value displayed but when submit to server it had "0" not the counted value.

在控制器中,我拥有

$scope.form={};

感谢任何帮助,这是AngularJS的新功能.

Any help appreciated, new to AngularJS.

推荐答案

您不需要 ng-change .您可以只观看变量并使用 $ scope.$ watch 更新它.您也可以从输入中删除 ng-init 和 ng-value .

You don't need ng-change. You can just watch the variable and update it using $scope.$watch. You can remove ng-init and ng-value from your inputs too.

var myApp = angular.module('myApp', []); myApp.controller('ctrl', ['$scope', function ($scope) { $scope.form={}; $scope.$watch('form.field1 + form.field2', function (value) { $scope.form.field3 = value; }); }]);

<script src="ajax.googleapis/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="ctrl"> <input type="number" ng-model="form.field1" /> <input type="number" ng-model="form.field2"/> <input type="number" ng-model="form.field3"/> </div>

更多推荐

如何设置从其他两个数字字段计算出的angularjs模型值

本文发布于:2023-11-22 13:56:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   计算出   如何设置   模型   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!