如何设置从其他两个数字字段计算出的angularjs模型值 [英] How to set angularjs model value calculated from other two number fields

查看:167
本文介绍了如何设置从其他两个数字字段计算出的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="https://ajax.googleapis.com/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模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆