【发布时间】:2017-11-17 05:42:57
【问题描述】:
http://plnkr.co/edit/iVvvdbvI3it7KrYZW6mK
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.selectModel = '1';
$scope.inputModel = new Date();
$scope.testOptions = [
{key: '1', description: 'text 1'},
{key: '2', description: 'text 2'},
{key: '3', description: 'text 3'}
];
});
.
<body ng-controller="MainCtrl">
<form name="inputForm">
<strong>how $viewValue works for input</strong><br />
<input type="date" name="input" ng-model="inputModel"/>
<div>$viewValue: {{inputForm.input.$viewValue}}</div>
<div>$modelValue: {{inputForm.input.$modelValue}}</div>
</form>
<br /><br /><br />
<form name="selectForm">
<strong>how $viewValue works for select</strong><br />
<select name="select" ng-model="selectModel" ng-options="item.key as item.description for item in testOptions"></select>
<div>$viewValue: {{selectForm.select.$viewValue}}</div><!--shows '1', i expected 'text 1'-->
<div>$modelValue: {{selectForm.select.$modelValue}}</div><!--shows '1', as expected-->
</form>
</body>
在上面的 plunker 中,你可以看到 $viewValue 对于 input 和 select 的不同含义。
对于输入,$viewValue 是用户看到的字符串,$modelValue 是数据模型。
对于 select,$viewValue 和 $modelValue 是相等的,都是数据模型。 这对我来说没有任何意义。这是为什么呢?
澄清一下,我的问题是关于 select 元素,具体来说:$viewValue 来自哪里(它似乎总是等于 $modelValue),为什么它不是实际显示在 select 元素中的文本?
【问题讨论】:
标签: angularjs