当我单击“保存”时,出现问题标题错误“TypeError:无法读取未定义的属性‘nombre’”。我正在复制 html 和 Controller js
当我单击“保存”时,出现问题标题错误“TypeError:无法读取未定义的属性‘nombre’”。我正在复制 html 和 Controller js
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8">
<link href="style.css" rel="stylesheet">
<title>.: IceDreamApp :.</title>
<body ng-controller="sueniosLista">
<h1>Lista de sueños</h1>
<div class="wrapper">
<div class="suenio-item" ng-repeat="suenio in suenios">
<div class="nombre"> {{suenio.nombre}} - {{suenio.suenioDescription}}</div>
<span class="estadoAnimo"> {{suenio.animo}}</span>
</div>
</div>
<div class="wrapper">
<h2>Añadir sueño</h2>
Nombre: <input type="text" ng-model="sueniosLista.nombre"></input>
Que soñaste?: <input type="text" ng-model="sueniosLista.suenioDescription"></input>
Cual fue tu estado de ánimo?: <input type="text" ng-model="sueniosLista.animo"></input>
<button ng-click="Save()">Guardar</button>
</div>
<script src="angular.min.js" type="text/javascript"></script>
<script src="scripts.js" type="text/javascript"></script>
</body>
</html>
function sueniosLista ($scope){
$scope.suenios= [
{nombre: "Daniel Galan Romano", suenioDescription: "Soñe que me comia un tiburon que estaba re loco porque no sabia que mierda hacer porque estaba solo en altamar", animo: "miedoso"},{nombre: "Dante", suenioDescription: "Soñe que me comia un tiburon que estaba re loco porque no sabia que mierda hacer porque estaba solo en altamar", animo: "miedoso"}
];
$scope.Save=function(){
$scope.suenios.push({nombre:$scope.nuevoSuenio.nombre, suenioDescription:$scope.nuevoSuenio.suenioDescription, animo:$scope.nuevoSuenio.animo })
}
}
最佳答案
在你的保存函数中有这一行:
$scope.suenios.push({
nombre: $scope.nuevoSuenio.nombre,
suenioDescription: $scope.nuevoSuenio.suenioDescription,
animo: $scope.nuevoSuenio.animo
});
您的代码中没有将 nuevoSuenio
定义为模型。我认为你应该将其更改为 sueniosLista
,例如:
$scope.suenios.push({
nombre: $scope.sueniosLista.nombre,
suenioDescription: $scope.sueniosLista.suenioDescription,
animo: $scope.sueniosLista.animo
});
关于javascript - 类型错误 : Cannot read property 'nombre' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36768880/