Angularjs ng单击第二次单击时的功能评估(或,取消单击) --HTML--- {{main}} 测试:{{mainDish}} ---JS--- $scope.setDish=函数(){ 如果($scope.selectedMain=='Other'){ $scope.mainDish=$scope.main其他文本; } 否则{ $scope.mainDish=$scope.selectedMain; } };
我有一个用于选择菜单项的单选按钮网格,最后一个是“其他”。当选择“其他”时,将显示一个文本框,用户在其中输入另一项。我使用ng click函数的目标是在选择“other”时,使用other文本框中的值更新变量mainDish 此代码起作用,但问题在于变量mainDish仅在取消选中单选按钮时更新 示例-当第一次单击按钮时,它对变量mainDish没有影响,但当单击另一个按钮时,mainDish将更新为单击的第一个按钮的值 有没有关于为什么会发生这种情况的想法,或者我可以如何解决它 编辑: 根据评论,我将进一步解释我的代码。我有一个菜单,一个用户从中选择的膳食列表。此列表由ng repeat命令填充,该命令从一组食物中抓取,并为每个食物创建一个单选按钮。数组中的最后一项是名为“Other”的项。选择“其他”后,会出现一个文本框,用户可以输入自己的菜肴 我想要的:当选择“其他”单选框时,我想要为变量mainDish分配“其他”文本框的值。对于所有其他项目,mainDish的值只是单选按钮的值,但“other”的情况并非如此Angularjs ng单击第二次单击时的功能评估(或,取消单击) --HTML--- {{main}} 测试:{{mainDish}} ---JS--- $scope.setDish=函数(){ 如果($scope.selectedMain=='Other'){ $scope.mainDish=$scope.main其他文本; } 否则{ $scope.mainDish=$scope.selectedMain; } };,angularjs,angularjs-ng-click,Angularjs,Angularjs Ng Click,我有一个用于选择菜单项的单选按钮网格,最后一个是“其他”。当选择“其他”时,将显示一个文本框,用户在其中输入另一项。我使用ng click函数的目标是在选择“other”时,使用other文本框中的值更新变量mainDish 此代码起作用,但问题在于变量mainDish仅在取消选中单选按钮时更新 示例-当第一次单击按钮时,它对变量mainDish没有影响,但当单击另一个按钮时,mainDish将更新为单击的第一个按钮的值 有没有关于为什么会发生这种情况的想法,或者我可以如何解决它 编辑: 根据评
我所拥有的:它有效!但奇怪的是。仅当取消选择单选按钮时,变量mainDish才会更新。每次单击新单选按钮时,变量mainDish都会被分配上一个按钮的值。这是我需要帮助解决的问题。问题是在更新范围的
ng模型
代码之前触发ng单击
。如果您将其更改为使用ng change
,这将修复它。您还需要在文本框中添加ng change
,以便在用户键入时更新范围
--- HTML ---
<div class="btn-group menuSection">
<div class="menuGrid" ng-repeat="main in mains">
<button type="button" class="btn btn-default btn-sm btn-block"
ng-value="main" btn-radio="main" ng-model="$parent.selectedMain"
ng-click="setDish();"/>
{{main}}
</div>
<div class="menuGrid">
<input type="text" class="form-control input-sm"
ng-model="mainOtherText" ng-show="selectedMain == 'Other'"
placeholder="enter other here"/>
</div>
test : {{mainDish}}
</div>
--- JS ---
$scope.setDish = function(){
if ($scope.selectedMain == 'Other') {
$scope.mainDish = $scope.mainOtherText;
}
else {
$scope.mainDish = $scope.selectedMain;
}
};
{{main}}
测试:{{mainDish}}
下面是一个工作示例:欢迎使用StackOverflow!这个问题看起来经过深思熟虑,但似乎缺少了创建完整工作示例所需的一些关键数据。您可以考虑在代码上更详细地更新问题,尤其是编辑<代码>主代码< /代码>。如果你需要更多的信息,请告诉我。我认为问题不在主阵列内。我认为问题要么在于ng click,要么在于我编写的函数。如果您有任何见解,我们将不胜感激。它实际上可能与您的HTML。。。不是一个自动关闭标签,它必须是…我的答案基本相同,但我没有考虑使用
ng change
,这似乎比我试图提供的解决方案更有效。这就是我需要的,谢谢!现在一切正常。
<div class="btn-group menuSection">
<div class="menuGrid" ng-repeat="main in mains">
<input type="radio" name="dishes"
class="btn btn-default btn-sm btn-block"
ng-value="main"
ng-model="$parent.selectedMain"
ng-change="setDish();"/>
{{main}}
</div>
<div class="menuGrid">
<input type="text" class="form-control input-sm"
ng-model="mainOtherText"
ng-show="selectedMain == 'Other'"
ng-change="setDish();"
placeholder="enter other here"/>
</div>
test : {{mainDish}}
</div>