[英]AngularJS revert selected value of control nested in ng-repeat on dialog cancel
I want to revert a select tag value that is inside a ng-repeat after cancelling a confirm dialog.我想在取消确认对话框后恢复ng-repeat 中的选择标记值。
Here is what I have so far:这是我到目前为止所拥有的:
Relevant HTML :相关 HTML :
<table>
<tbody>
<tr ng-repeat="application in rows">
<td>
<select
ng-model="application.selectedVersion"
ng-options="apk.versionName for apk in application.versions | orderBy : 'id' : true"
ng-init="application.selectedVersion=application.versions.filter(currentVersion, application.apkUpdates)[0]"
ng-change="selectionChanged(application, '{{application.selectedVersion}}')"
style="padding:0 1em;" />
</td>
</tr>
</tbody>
</table>
Javascript logic : Javascript 逻辑:
$scope.selectionChanged = function(application, previousVersion) {
var dialog = confirm('Change version?');
if (dialog) {
console.log('change version confirmed');
} else {
application.selectedVersion = previousVersion;
}
};
Passing '{{application.selectedVersion}}'
to the function instead of application.selectedVersion
passes the previously selected value instead of the current (explained here: https://stackoverflow.com/a/45051464/2596580 ).将
'{{application.selectedVersion}}'
传递给函数而不是application.selectedVersion
传递先前选择的值而不是当前值(在此处解释: https : //stackoverflow.com/a/45051464/2596580 )。
When I change the select value, perform the dialog interaction and cancel it I try to revert the value by setting application.selectedVersion = angular.copy(previousVersion);
当我更改选择值时,执行对话框交互并取消它我尝试通过设置
application.selectedVersion = angular.copy(previousVersion);
来恢复该值application.selectedVersion = angular.copy(previousVersion);
. . I can see the value is correct by debugging the javascript but the select input is set to blank instead of the actual value.
我可以通过调试 javascript 看到该值是正确的,但选择输入设置为空白而不是实际值。
What am I doing wrong?我究竟做错了什么?
JSFiddle demo: https://jsfiddle.net/yt4ufsnh/ JSFiddle 演示: https ://jsfiddle.net/yt4ufsnh/
You have to correct couple of things in your implementation你必须在你的实现中纠正几件事
'{{application.selectedVersion}}'
to selectionChanged
method, it becomes raw string
.'{{application.selectedVersion}}'
传递给selectionChanged
方法时,它会变成原始string
。 When you re assigns back to the application.selectedVersion
you have to first parse that previousVersion
to JSON
using JSON.parse
methodapplication.selectedVersion
您必须首先使用JSON.parse
方法将该previousVersion
解析为JSON
track by apk.id
on ng-options
collection.ng-options
集合上使用track by apk.id
。 This is needed because the JSON parsed object is not recognized as the same instance of the object used to build the select, so this works as if overriding an intrinsic equals
function to use only its id propertyequals
函数以仅使用其 id 属性一样Final Version最终版本
ng-options="apk.versionName for apk in (application.versions |
orderBy : 'id' : true) track by apk.id"
Code代码
$scope.selectionChanged = function(application, previousVersion) {
var dialog = confirm('Change version?');
if (dialog) {
console.log('change version confirmed');
} else {
application.selectedVersion = previousVersion ? JSON.parse(previousVersion) : null;
}
};
if you just remove the else condition from your javascript code, you can reach the behaviour you need.如果你只是从你的 javascript 代码中删除 else 条件,你就可以达到你需要的行为。
Your final code should be:你的最终代码应该是:
$scope.selectionChanged = function(application, previousVersion) {
var dialog = confirm('Change version?');
if (dialog) {
console.log('change version confirmed');
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.