gpt4 book ai didi

javascript - 无法读取未定义的属性 'push'。将数据添加到数组中。

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

我刚刚开始学习 AngularJS,我想从表单传递信息,然后传递到 Controller 并将信息输入到数组中,但收到此消息“无法读取未定义的属性‘push’”。我不知道我做错了一部分。我希望你们能帮我解决这个问题。提前谢谢你们。

form.html

<!DOCTYPE html>
<html ng-app="formTraining">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="formapp.js"></script>
<link rel="stylesheet" href="style.css" />
</head>

<body ng-controller="UserController as usersCtrl">

<h3>User Information</h3>
<div ng-repeat="user in usersCtrl.mUsers">
<p><ul>
<li>{{user.name}}</li>
<li>{{user.age}}</li>
<li>{{user.occupation}}</li>
</ul></p>
</div>

<div ng-controller="FormController as formCtrl">

<form name="form" ng-submit="form.$valid && formCtrl.addUser(user)">
<h5>Submit a Review</h5>

<fieldset class="form-group">
<input type="text" ng-model="UserCtrl.newUser.name" class="form-control" placeholder="write your name.." title="Name">
</fieldset>

<fieldset class="form-group">
<input type="text" ng-model="UserCtrl.newUser.age" class="form-control" placeholder="write you actual age.." title="Age">
</fieldset>

<fieldset class="form-group">
<input type="text" ng-model="UserCtrl.newUser.occupation" class="form-control" placeholder="write your job.." title="occupation">
</fieldset>

<fieldset class="form-group">
<input type="submit" class="btn btn-primary pull-right" value="submit form">
</fieldset>
</form>
</div>

</body>

</html>

formapp.js

(function(){


var app = angular.module("formTraining",[]);



app.controller('UserController',function(){

this.mUsers = users;
});

app.controller('FormController',function(){
this.newUser = {};

this.addUser = function(user){
user.push(this.newUser);
this.newUser = {};
};
});


var users = [{
name:"michael",
age:"27",
occupation:"business"
},{
name:"john",
age:"25",
occupation:"police"
}];

})();

最佳答案

你不想要用户

    app.controller('FormController',function(){
this.newUser = {};

this.addUser = function(user){
users.push(this.newUser);
^

this.newUser = {};
};
});

关于javascript - 无法读取未定义的属性 'push'。将数据添加到数组中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48916974/

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