我有一个使用angularJs的表单,我主要关注的是带有ng-option的select标签 让我快速看一下表格,然后发生的事情将解释我的问题。 注意:我删除了不必要的部分,如div和类,因为它不是一个leyout问题。
<form role="form" name="feedbackForm" ng-submit="sendFeedback()" novalidate>
<!-- ---------------- SELECT ------------------->
<label>
<input type="checkbox" name="approve" value="" ng-model="feedback.agree">
<strong>May we contact you?</strong>
</label>
<div ng-show="feedback.agree" ng-model="feedback.myChannel">
<select class="form-control c-select" name="select" ng-model="feedback.myChannel" ng-options="channel.value as channel.label for channel in channels">
<option value="">Select a method</option>
</select>
</div>
<!-- --------------------TEEPHONE ---------------------->
<label for="telnum">Contact Tel.</label>
<input type="tel" id="areacode" name="areacode" ng-model="feedback.tel.areacode">
<input type="tel" id="telnum" name="telnum" ng-model="feedback.tel.number">
<!-- ------------- EMAIL ------------------------>
<label for="emailid" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" id="email" name="email" ng-model="feedback.email" >
然后我只是为了开发目的而显示我的值
<p> Contact by: {{ feedback.myChannel }} </p>
<p> feedback.agree: {{'( ' + feedback.agree + ' )'}} </p>
<p> feedback.myChannel: {{'( ' + feedback.myChannel + ' )'}} </p>
在控制器和ng-option中我有:
$scope.channels = [
{ value:"Tel", label:"Tel" },
{ value:"Email", label:"Email" },
{ value:"Tel & Email", label:"Tel & Email" }
];
然后将反馈推送到服务器我创建了这个目标:
$scope.feedback = { myChannel:"", firstName: '', lastName: '', agree: false, email: '' };
这里有一些图片显示所有这些变量和值都正确地提供给网站。
我的问题是:
我尝试使用$scope.feedback.myChannel
来满足其他需求,但我无法在我的控制器上找到,而我却在我的表单中使用它。
此行没有任何内容。也许是一个空字符串:
console.log('myChannel = ' + $scope.feedback.myChannel);
所有这些如果不起作用且控制台中没有显示任何内容:
if($scope.feedback.myChannel == "Tel" || $scope.feedback.myChannel === "Tel & Email"){
console.log($scope.feedback.myChannel);
};
if($scope.feedback.myChannel === "Email" || $scope.feedback.myChannel === "Tel & Email"){
console.log($scope.feedback.myChannel);
};
这个人没有做它想做的事情:
if(!$scope.feedback.agree){ $scope.feedback.myChannel = ''; };
此console.log提供了意外值:
console.log('1: ' + $scope.feedbackForm.myChannel) // undefined
console.log('2: ' + $scope.feedback.myChannel); // nothing .. perhaps an empty string