Angular将数字添加到数组中

时间:2016-12-07 13:56:53

标签: javascript angularjs function

我是棱角分明的新手,我试图制作一个添加字符表单字段,但我做错了什么

这是我的带有角度

的html表单
<form class="add_character" ng-submit="characterController.addCharacter(characters)">
          <input ng-model="addCharacterCtrl.character.name" type="text">
          <input ng-model="addCharacterCtrl.character.last_name" type="text">
          <input ng-model="addCharacterCtrl.character.age" type="text">
          <textarea ng-model="addCharacterCtrl.character.history" name="" id="" cols="30" rows="10"></textarea>
          <input type="submit">
      </form>

这是我的控制器,其中包含我的添加字符功能:

app.controller('CharacterController', function() {
this.characters = personages;


this.addCharacter = function(newCharacter){
  addCharacter.push(this.characters);

  this.addCharacter = [];
};
  });



  var personages = [
{
  name: 'Character',
  last_name: 'character',
  age: 21,
  history: 'character info',
},
{
  name: 'Character2',
  last_name: 'character',
  age: 44,
  history: 'character info',
}


  ];

这是我得到的错误:

  

Blockquote angular.js:13920 TypeError:无法读取属性&#39; push&#39;未定义的       at Object.addCharacter(addCharacter.js:11)       at fn(eval at compile(angular.js:14817),:4:394)       at b(angular.js:15906)       在e(angular.js:25885)       at m。$ eval(angular.js:17682)       at m。$ apply(angular.js:17782)       在HTMLFormElement。 (angular.js:25890)       在Sf(angular.js:3497)       在HTMLFormElement.d(angular.js:3485)

我希望有人可以帮助我

3 个答案:

答案 0 :(得分:2)

您必须使[{1}}和addCharacter像[]一样,而不是Array,如{}。

所以写:

Object

而不是:

this.addCharacter = [];

答案 1 :(得分:1)

以下是我看到的一些内容:

  

this.addCharacter = {};

您将addCharacter初始化为对象。

  

this.addCharacter = function(addCharacter){         addCharacter.push(this.personages);       };

接下来,您将其重新定义为函数。

  

addCharacter.push(this.personages);

最后,您尝试使用Array.push将字符插入对象中。此外,您在addCharacter的方法签名中使用相同的变量名称。

答案 2 :(得分:1)

在您收到的错误之前回答是因为您正在尝试将对象推入另一个对象,因此将其更改为数组

此外,您的标记和代码有点过头了,这是一个简化版本:

app.controller('CharacterController', function($scope) {

  // The initial list of characters
  // Here I declare the personages as a scope variable as I am listing them in the view (see plunker below), but you could just declare it as a normal variable
  $scope.personages = [ 
    { name: 'Character', last_name: 'character', age: 21, history: 'character info'},
    { name: 'Character2', last_name: 'character', age: 44, history: 'character info'}
  ];

  // An object to hold the new character data, use this in your form model
  $scope.newCharacter = {};

  // The form submit function which receives its model
  $scope.addCharacter = function(character) {
    $scope.personages.push(character);
  }
});

<form class="add_character" ng-submit="addCharacter(newCharacter)">
      <input ng-model="newCharacter.name" type="text">
      <input ng-model="newCharacter.last_name" type="text">
      <input ng-model="newCharacter.age" type="text">
      <textarea ng-model="newCharacter.history" name="" id="" cols="30" rows="10"></textarea>
      <input type="submit">
    </form>

预览:

https://plnkr.co/edit/iDVPdKm7d96XR1Sk6uQh?p=preview