在javascript中替换当前数据时出错
我需要替换此表中从用户处获取的当前数据。但是,它没有替换当前数据,而是为我提供了新的数据行在javascript中替换当前数据时出错,javascript,angularjs,Javascript,Angularjs,我需要替换此表中从用户处获取的当前数据。但是,它没有替换当前数据,而是为我提供了新的数据行 $scope.users = [{username: "a", name:"b", status:"0", scope:"1"}]; $scope.addUser = function(user) { $dialog.open({ showClose: false, closeByEscape: true,
$scope.users = [{username: "a", name:"b", status:"0", scope:"1"}];
$scope.addUser = function(user) {
$dialog.open({
showClose: false,
closeByEscape: true,
template: 'views/user/user-user-add.html',
controller: ['$scope', function ($dialogScope) {
$dialogScope.title = "New User";
$dialogScope.user = {
username : "" ,
name : "",
status : "",
scope : "",
};
if(user){
$dialogScope.title = "Update User";
$dialogScope.user = {
username :user.username ,
name :user.name ,
status : user.status,
scope : user.scope,
};
}
$dialogScope.add = function() {
$scope.users.push($dialogScope.user);
$dialogScope.closeThisDialog();
}
}],
});
};
问题是这个代码
$dialogScope.user = {
username: user.username ,
name: user.name ,
status: user.status,
scope: user.scope,
};
$scope.users.push($dialogScope.user);
…正在范围上创建user
对象。那么这个代码
$dialogScope.user = {
username: user.username ,
name: user.name ,
status: user.status,
scope: user.scope,
};
$scope.users.push($dialogScope.user);
…正在将对该对象的引用推送到用户
数组中。现在,以后任何时候修改$dialogScope.user
时,您都在修改推送到用户
数组的引用,而不是创建新的用户
。你应该做的是
$scope.users.push(angular.copy($dialogScope.user));
…创建一个新的user
实例,并将其推送到users
数组中。然后您可以使用清除对话框中的值
$dialogScope.user = {};
您能否提供前端代码、对话框部分。@Nivesh为您提供plunker链接。嘿,我不明白你的意思。您正在执行addUser和
$scope.users。push肯定会将新用户添加到您的用户数组中
现在您必须替换哪些数据?从表中显示的用户数组中获取的旧用户。如果是,那么听起来更像是更新用户而不是添加用户。我需要替换用户输入的数据。这就是为什么我使用if(user){$dialogScope.user={username:username…}}。现在的问题是,@Nivesh表中的数据没有改变