gpt4 book ai didi

javascript - AngularJS - 将 $scope.data 重置为原始值

转载 作者:行者123 更新时间:2023-12-03 02:56:05 25 4
gpt4 key购买 nike

我在这里创建了一个 fiddle :http://jsfiddle.net/nicktest2222/W4VaA/

我只是希望能够按下重置按钮并恢复原始值。有谁知道执行此操作的最佳方法吗?

提前致谢

function TodoCtrl($scope) {
$scope.data = [
{text:'learn angular', done:true},
{text:'build an angular app', done:false}];

$scope.orig = [$scope.data];

$scope.reset = function() {
$scope.data = $scope.orig;
};

}

最佳答案

The problem is in JS clone mechanics 。您需要做的就是创建模型的深拷贝:

function TodoCtrl($scope) {
$scope.data = [
{text:'learn angular', done:true},
{text:'build an angular app', done:false}
];

$scope.orig = angular.copy($scope.data);

$scope.reset = function() {
$scope.data = angular.copy($scope.orig);
};
}

Here is the updated fiddle.

关于javascript - AngularJS - 将 $scope.data 重置为原始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17559258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com