繁体   English   中英

AngularJS无法读取未定义错误的属性“ push”

[英]AngularJS Cannot read property 'push' of undefined error

我正在尝试构建一个简单的电话簿Web应用程序来帮助我学习角度,但我陷入了该错误-无法读取未定义错误的属性“ push”。 谁能指出这是怎么回事?

是我要推送的目标变量或源错误?

JS:

  (function() { var app = angular.module('PhonebookApp', []); app.controller('BookController', function() { this.contacts = [{ firstName: "Contact1", lastName: "LastName1", phone: 123123123123, notes: "None" }]; }); app.controller('ContactController', function() { //this.contact = {}; this.contact = { firstName: "Tim2", lastName: "Last2", phone: 12312312312, notes: "notessss" }; this.addContact = function(contacts) { contacts.push(this.contact); }; }); })(); 

HTML:

 <section ng-controller="BookController as bookCtrl"> <table> <tr> <td>First Name</td> <td>Last Name</td> <td>Phone Number</td> <td>Notes</td> </tr> <tr ng-repeat="contact in bookCtrl.contacts"> <td>{{ contact.firstName }}</td> <td>{{ contact.lastName }}</td> <td>{{ contact.phone }}</td> <td>{{ contact.notes }}</td> </tr> </table> </section> <section> <div class="form_addContact"> <form ng-show="showForm" ng-controller="ContactController as contactCtrl" ng-submit="contactCtrl.addContact(bookCtrl.contacts)" novalidate> <label> First Name: <input type="text" required ng-pattern="/^(\\D)+$/" ng-model="contactCtrl.contact.firstName"></input> <p class="inputErr" ng-show="contactCtrl.contact.firstName.$error.pattern">First name must contain letters only!</p> </label> <label> Last Name: <input type="text" ng-pattern="/^(\\D)+$/" ng-model="contactCtrl.contact.lastName"></input> <p class="inputErr" ng-show="contactCtrl.contact.lastName.$error.pattern">Last name must contain letters only!</p> </label> <label> Phone Number: <input type="number" required ng-model="contactCtrl.contact.phone"></input> </label> <label> Notes: <input type="text" ng-model="contactCtrl.contact.notes"></input> </label> <input type="submit" value="Submit" /> </form> 

<form ng-show="showForm" ng-controller="ContactController as contactCtrl" ng-submit="contactCtrl.addContact(bookCtrl.contacts)" novalidate>

这里没有定义bookCtrl ,因为您不在其中。 Angular不会因执行undefined.something类的代码而触发任何错误。 因此,当您在addContact输入时, contacts只有一个未定义的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM