gpt4 book ai didi

javascript - AngularJS - 将数据附加到 json 数组

转载 作者:行者123 更新时间:2023-11-30 17:29:31 24 4
gpt4 key购买 nike

我遇到了一种情况,我必须将数据从一个 Controller 传递到另一个 Controller 。

我的应用程序是这样工作的:

View 1 -> 从json数组中获取一些用户并显示在表格中。

当我在 View 1 中单击添加用户时,我被重定向到 View 2。

View 2 -> 有 2 个输入字段和一个按钮(用于添加新用户)。

当我重定向到 View 1 时,我希望添加新用户,但这不起作用。

为了简单起见,我在这里进行了演示 - http://plnkr.co/edit/yz6mpplZGh4q5bPDO2bc?p=preview

非常感谢任何帮助。

最佳答案

您总是在列表 Controller 的条目上覆盖“用户”内容。

将您的 Controller 更改为

    ...
controller('UserController', function($scope,UserService){

if (!$scope.users)
{
UserService.getUsers().then(function(data){
$scope.users = data;
})
}
...

并带回你的“正确”

 $scope.users.push($scope.user)

在您的 addUserController 中。

关于javascript - AngularJS - 将数据附加到 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443414/

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