Angularjs 无法获取要填充表的输入表单项
当我学习angular.js时,我目前正在构建一个简单的小应用程序,并且在使用用户键入的数据填充表时遇到了一些问题。我收到一个错误:Angularjs 无法获取要填充表的输入表单项,angularjs,html,Angularjs,Html,当我学习angular.js时,我目前正在构建一个简单的小应用程序,并且在使用用户键入的数据填充表时遇到了一些问题。我收到一个错误: Cannot read property 'push' of undefined 报告如下: var app = angular.module('yardApp', []); app.controller('mainController', function($http){ var yardSale = this; yardSale.forSale =
Cannot read property 'push' of undefined
报告如下:
var app = angular.module('yardApp', []);
app.controller('mainController', function($http){
var yardSale = this;
yardSale.forSale = [];
yardSale.login = function(){
yardSale.loggedIn = true;
$http({
method: "POST",
url: "/user",
data: { username:yardSale.username }
}).then(function(result){
console.log(result.data);
yardSale.userId = result.data._id;
yardSale.userId = result.data.username;
});
};
yardSale.addItem = function(){
console.log(yardSale.item.product);
yardSale.items.push(yardSale.item);
// yardSale.item = {};
};
}):
html看起来像:
<div>
<h1>Welcome to the Yard Sale!!</h1>
<h2 ng-hide="yardSale.loggedIn"> Plese login to get started.</h2>
<h2 ng-show="yardSale.loggedIn">Welcome {{ yardSale.username }}!</h2>
<h2 ng-show="yardSale.loggedIn">Have anything you'd like to sell?</h2>
<h3 ng-show="yardSale.loggedIn">Please, list it here!</h3>
</div>
<form ng-submit="yardSale.login()" ng-hide="yardSale.loggedIn">
<input type="text" placeholder="Please enter a username" ng-model="yardSale.username">
<input class="btn btn-primary" type="submit" value="login">
</form>
<div class="row" ng-show="yardSale.loggedIn">
<form ng-submit="yardSale.addItem()">
<input type="text" placeholder="Item for Sale" ng-model="yardSale.item.product">
<input type="text" placeholder="Price you'd like" ng-model="yardSale.item.price">
<input type="date" placeholder="Date Listed" ng-model="yardSale.item.date">
<input class="btn btn-primary" type="submit" value="add">
</form>
<table class="col-md-6 col-md-offset-4 table-bordered">
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Date Listed</th>
</tr>
</thead>
<tbody>
<tr ng-repeat=" item in yardSale.items">
<td>{{ item.product }}</td>
<td>{{ item.price | currency }}</td>
<td>{{ item.date | date }}</td>
</tr>
</tbody>
</table>
</div>
欢迎来到庭院大拍卖!!
请登录以开始。
欢迎{{yardSale.username}}!
你有什么要卖的吗?
请把它列在这里!
项目
价格
列出的日期
{{item.product}}
{{item.price | currency}}
{{item.date | date}}
我可以控制台记录yardsale.item.product并查看我输入的内容,但无法将其填充到表中由于名为
items
的变量,因此出现错误无法读取未定义的属性push。尝试在构造函数的这行代码之后添加yardSale.items=[]
var yardSale = this;
yardSale.forSale = [];
yardSale.items = [];
由于名为items
的变量,出现错误无法读取未定义的属性push。尝试在构造函数的这行代码之后添加yardSale.items=[]
var yardSale = this;
yardSale.forSale = [];
yardSale.items = [];
当您试图访问未声明的变量值时,会出现此错误。在这种情况下,yardSale.items
从不声明。编译器应该知道对象yardSale存在这样一个属性,以及该属性(即items)是对象还是数组。因此@amcpanaligan的答案是正确的。您需要将其声明为:
yardSale.items = [];
尝试:
yardSale.items.push(yardSale.item);
它会起作用的
当您试图访问未声明的变量值时,会出现此错误。在这种情况下,yardSale.items
从不声明。编译器应该知道对象yardSale存在这样一个属性,以及该属性(即items)是对象还是数组。因此@amcpanaligan的答案是正确的。您需要将其声明为:
yardSale.items = [];
尝试:
yardSale.items.push(yardSale.item);
会有用的。非常感谢!非常感谢你!感谢您澄清@amcpanaligan的解决方案是正确的原因。我有点困惑。感谢您澄清@amcpanaligan的解决方案是正确的原因。我有点困惑。