我正在尝试将html表单元素中的注释添加到angularjs中的数组中。当我使用javascipt push函数时,我得到一个错误"对象的属性不存在"。 有人可以帮我解决这个问题。您可以在下面找到我的javascript和html代码:
感谢您阅读
.controller('DishDetailController', ['$scope', function($scope) {
var dish={
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:4,
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
date:"2014-09-05T17:57:28.556094Z"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}
]
};
$scope.dish = dish;
}])
.controller('DishCommentController', ['$scope', function($scope) {
$scope.newcomment = {
rating : "",
comment: "",
author: "",
date: new Date().toISOString()
};
$scope.submitComment = function () {
//Step 2: This is how you record the date
$scope.dish.comments.push($scope.newcomment);
}
}])
;

<!DOCTYPE html>
<html lang="en" ng-app="confusionApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="../bower_components/bootstrap/dist/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet">
<link href="styles/bootstrap-social.css" rel="stylesheet">
<link href="styles/mystyles.css" rel="stylesheet">
<!-- endbuild -->
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="row row-content" ng-controller="DishDetailController">
<div class="media" ng-model="dish">
<div class="media-left media-middle">
<a href="#">
<img class="media-object img-thumbnail" ng-src={{dish.image}} alt="Uthapizza">
</a>
</div>
<div class="media-body">
<h2 class="media-heading"> {{dish.name}}
<span class="label label-danger">
{{dish.label}}
</span>
<span class="badge">
{{dish.price | currency}}
</span>
</h2>
<p>{{dish.description}}</p>
</div>
</div>
</div>
<br>
<br>
<br>
<div class="col-xs-9 col-xs-offset-1" ng-controller="DishDetailController">
<h4>Customer Comments</h4> Sort by:
<input type="text" ng-model="filterText">
<blockquote ng-repeat="commentla in dish.comments | orderBy: filterText">
<p class="mb-0"> {{commentla.rating}} Stars </p>
<p class="mb-0">{{commentla.comment}}</p>
<footer class="blockquote-footer"> {{commentla.author}} {{commentla.date | date: 'mediumDate'}} </footer>
</blockquote>
</div>
</div>
<div class="col-xs-9 col-xs-offset-1" ng-controller="DishCommentController">
<ul class="list-unstyled">
Your Name: {{newcomment.author}}
<br>
Comment: {{newcomment.comment}}
<br>
Your Rating: {{newcomment.rating}}
<!-- <p>Task 3: Here you include the code to show the live preview of the comment</p>
<p>The comment should be shown only when the form contains valid
information and is not pristine</p> -->
</ul>
<form class="form-horizontal" name="commentForm" ng-submit="submitComment()" novalidate>
<div class="form-group">
<label for="yname" class="col-sm-2 control-label">Your Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="yname" aria-des <div class="col-sm-10">
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="1" ng-model="newcomment.rating"> 1
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="2" ng-model="newcomment.rating"> 2
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="3" ng-model="newcomment.rating"> 3
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio4" value="4" ng-model="newcomment.rating"> 4
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio5" value="5" ng-model="newcomment.rating"> 5
</label>
</div>
</div>
<div class="form-group">
<label for="commentm" class="col-sm-2 control-label">Your Comment</label>
<div class="col-sm-10">
<textarea class="form-control" id="commentm" name="commentm" rows="12" ng-model="newcomment.comment"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" ng-disabled="feedbackForm.$invalid">Submit Comment</button>
</div>
</div>
</form>
</div>
</div>
<!-- build:js scripts/main.js -->
<script src="../bower_components/angular/angular.min.js"></script>
<script src="scripts/app.js"></script>
<!-- endbuild -->
</body>
</html>
&#13;
答案 0 :(得分:0)
我认为它与每个控制器的范围有关。当您的提交函数位于dish.comments
控制器中时,您在DishDetailController
中定义了DishCommentController
。
尝试将submitcomment
函数从DishCommentController
控制器转移到DishDetailController
控制器,因为DishCommentController
在其范围内甚至没有$ scope.dish.comments。
编辑:按照建议更新代码后,尝试一下
.controller('DishDetailController', ['$scope', function($scope) {
var $scope.dish ={
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
}
...
]
};
$scope.newcomment = {
rating : "",
comment: "",
author: "",
date: new Date().toISOString()
};
$scope.submitComment = function (rating , author, stars, date) {
$scope.dish.comments.push($scope.newcomment);
}
}]);
我删除了一些额外的代码,您可以在以后开始工作时添加这些代码。
<div class="row row-content" ng-controller="DishDetailController">
<div class="col-xs-9 col-xs-offset-1">
<h4>Customer Comments</h4> Sort by:
<input type="text" ng-model="filterText">
<blockquote ng-repeat="commentla in dish.comments | orderBy: filterText">
<p class="mb-0"> {{commentla.rating}} Stars </p>
<p class="mb-0">{{commentla.comment}}</p>
<footer class="blockquote-footer"> {{commentla.author}} {{commentla.date | date: 'mediumDate'}} </footer>
</blockquote>
</div>
<div class="col-xs-9 col-xs-offset-1">
<ul class="list-unstyled">
Your Name: {{newcomment.author}}
<br>
Comment: {{newcomment.comment}}
<br>
Your Rating: {{newcomment.rating}}
</ul>
<form class="form-horizontal" name="commentForm" ng-submit="submitComment()" novalidate>
<div class="form-group">
<label for="yname" class="col-sm-2 control-label">Your Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="yname" aria-des <div class="col-sm-10">
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="1" ng-model="newcomment.rating"> 1
</label>
..
</div>
<div class="form-group">
<label for="commentm" class="col-sm-2 control-label">Your Comment</label>
<div class="col-sm-10">
<textarea class="form-control" id="commentm" name="commentm" rows="12" ng-model="newcomment.comment"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" ng-disabled="feedbackForm.$invalid">Submit Comment</button>
</div>
</div>
</form>
</div>
</div>