Javascript angularjs数据绑定问题
我正在尝试允许用户提交评论,在那里可以显示它。但是,它似乎不能正常工作,因为我可以将其显示为预览,但当我提交时,它不会保存为保存其他注释的数组。调试后,我发现它绑定到了我的$scope.review,但它以网页上显示的空白默认格式提交到了我的数组中。源代码可以在上找到。(注释掉了原始状态和范围的重置,这也给出了相同的错误) JavascriptJavascript angularjs数据绑定问题,javascript,angularjs,Javascript,Angularjs,我正在尝试允许用户提交评论,在那里可以显示它。但是,它似乎不能正常工作,因为我可以将其显示为预览,但当我提交时,它不会保存为保存其他注释的数组。调试后,我发现它绑定到了我的$scope.review,但它以网页上显示的空白默认格式提交到了我的数组中。源代码可以在上找到。(注释掉了原始状态和范围的重置,这也给出了相同的错误) Javascript .controller('DishCommentController', ['$scope', function($scope) {
.controller('DishCommentController', ['$scope', function($scope) {
$scope.review = {name:"", rating:"Five", comment:"",date:""};
$scope.submitComment = function () {
$scope.review.date = new Date().toISOString();
// Push comment into the dish's comment array
$scope.dish.comments.push("$scope.review");
//reset form to pristine
// $scope.commentForm.$setPristine="";
//reset JavaScript object that holds your comment
// $scope.review = {name:"", rating:"Five", comment:"",date:""};
}
Html
{{review.rating}}星星
{{review.comment}
由{{review.name}在{{review.date | date:'mediumDate'}}上的{review.name}
您需要推送对象:
$scope.dish.comments.push($scope.review);
您正在推送字符串,而您需要推送对象:
$scope.dish.comments.push($scope.review);
另外,$scope.dish.comments
属于dishdailcontroller
。submitComment
方法属于控制器
。作用域是由控制器定义的,所以他们不知道彼此的存在
您需要将方法放在同一个控制器中如果您希望共享范围同意Lucas,您需要添加对象而不是字符串。此外,您的新审阅字段名称与现有审阅中的现有字段不匹配
.controller('DishCommentController', ['$scope', function($scope) {
$scope.review = {
rating:5,
comment:"",
author:"",
date:""
};
$scope.submitComment = function () {
$scope.review.date = new Date().toISOString();
// Push comment into the dish's comment array
$scope.dish.comments.push($scope.review);
//reset form to pristine
$scope.commentForm.$setPristine="";
//reset JavaScript object that holds your comment
$scope.review = {author:"", rating:5, comment:"",date:""};
}
}])
注意,我将“name”修改为“author”,将评级值从“Five”修改为5。您需要检查“预览”html中的绑定以匹配更改
修正了Pulks/P>谢谢,我通过与其他控制器匹配我的属性来修复错误。谢谢大家的帮助。
.controller('DishCommentController', ['$scope', function($scope) {
$scope.review = {
rating:5,
comment:"",
author:"",
date:""
};
$scope.submitComment = function () {
$scope.review.date = new Date().toISOString();
// Push comment into the dish's comment array
$scope.dish.comments.push($scope.review);
//reset form to pristine
$scope.commentForm.$setPristine="";
//reset JavaScript object that holds your comment
$scope.review = {author:"", rating:5, comment:"",date:""};
}
}])