Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs “如何映射一个”;模型";到两个领域?_Angularjs - Fatal编程技术网

Angularjs “如何映射一个”;模型";到两个领域?

Angularjs “如何映射一个”;模型";到两个领域?,angularjs,Angularjs,我有一个带有“高度”属性的模型,我想允许用户将其编辑为两个单独的字段“英尺”和“英寸”,但要将它们映射到以英寸为单位的单个属性“高度” 表单如下所示: 在这些字段和单个“height”属性之间创建双向绑定的最佳方法是什么?HTML: <input type="number" ng-model="feet"> <input type="number" ng-model="inches"> <br>{{height}} 我稍微修改了@Mark的示例,删除了$w

我有一个带有“高度”属性的模型,我想允许用户将其编辑为两个单独的字段“英尺”和“英寸”,但要将它们映射到以英寸为单位的单个属性“高度”

表单如下所示:

在这些字段和单个“height”属性之间创建双向绑定的最佳方法是什么?

HTML:

<input type="number" ng-model="feet">
<input type="number" ng-model="inches">
<br>{{height}}

我稍微修改了@Mark的示例,删除了$watch,我认为这是多余的

HTML



{{calc.height()}}
控制器

$scope.calc={
英尺:2,
英寸:5,
高度:函数(){返回this.feet*12+this.inches}
};

$scope.feet = 2;
$scope.inches = 5;
$scope.$watch('feet * 12 + inches', function(value){
  $scope.height = value;
})