gpt4 book ai didi

javascript - AngularJS 数组被重置

转载 作者:行者123 更新时间:2023-11-30 08:36:09 25 4
gpt4 key购买 nike

angularjs 有点新。我有以下代码,只是想知道是否有人不能告诉我为什么我的值没有保存在相应的数组中

<script type="text/javascript">
'use strict';
var app = angular.module('app', ['appControllers']);
"use strict";
var appControllers = angular.module('app', []);
appControllers.controller('personController', ['$scope', '$http', function ($scope, $http) {

$scope.responses = [];
$scope.coaches = [];
$scope.athletes = [];
$scope.staff = [];

$scope.addCoach = function() {
var coach = $scope.coach;
$scope.coach = {}; // Do this to clean up the form fields
$scope.coaches.push(coach);
//$scope.responses.push(coaches);
};

.....

$scope.addContact = function ($event) {

$event.preventDefault();
$scope.responses.push(coaches);
console.log("inside this method");

}

因此,我获取了已添加的教练列表,并试图将它们进一步插入响应数组。每次点击 addContact 的这个函数或者只是从原始函数中推送它时,它都会将响应数组返回为空。有什么见解吗?

最佳答案

您正在尝试推送 coaches 而不是 $scope.coaches

所以这就是你应该写的:

$scope.responses.push($scope.coaches); 

关于javascript - AngularJS 数组被重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31298890/

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