简体   繁体   中英

Getting Uncaught TypeError: Cannot read property 'push' of undefined

I'm getting the error when the page is loaded. I'm trying to append a new object to an array of entries. What's wrong with this?

在此处输入图片说明

raffle.js

angular.module('myApp', []).controller("RaffleCtrl", function ($scope) {
  $scope.entries = [
      {
        name: "Larry"
      }, {
        name: "Curly"
      }, {
        name: "Moe"
      }
    ]

  $scope.addEntry = function () {
    $scope.entries.push($scope.newEntry)
    $scope.newEntry = {}
  };

});

index.html

<h1>Raffler</h1>

<div ng-controller="RaffleCtrl">

  <form ng-sumbit="addEntry">
    <input type="text" ng-model="newEntry.name">
    <input type="submit" value="Add">
  </form>

  <ul>
    <li ng-repeat="entry in entries">{{entry.name}}</li>
  </ul>
</div>

You forgot to define $scope.newEntry = {} outside And parenthesis in form ng-sumbit="submit()" Modify your code to..

$scope.entries = [ { name: "Larry" }, { name: "Curly" }, { name: "Moe" } ];

  $scope.newEntry = {}  
   $scope.submit = function() {
     $scope.entries.push($scope.newEntry)
     $scope.newEntry = {}
   };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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