在AngularJS中,如何初始化位于另一个数组元素中的数组元素
How do I initialize an array element which is inside another array element in AngularJS?
$scope.pictures=[
{
name:"/images/profile2.jpg",
caption:"Food and Hunger",
votes:0,
User:"xyz@gmail.com",
comments:[],
theme:$scope.theme
},
{
name:"/images/profile3.jpg",
caption:"The wedding day",
votes:0,
User:"yamini@gmail.com",
comments:[],
theme:$scope.theme
},
{
name:"/images/profile4.jpg",
caption:"Mother's Care",
votes:0,
User:"fakeid@yahoo.com",
comments:[],
theme:$scope.theme
}];
"comments:[]"数组不起作用。当我尝试.push()函数时,它不起作用。然而,当我尝试push()对其他元素,如标题,用户等,然后它的工作。
$scope.addcomment=function (index) {
var com=$window.prompt('Please enter your comment');
$scope.pictures[index].comments.push(com);
}
有谁能帮我解决这个错误吗? 我已经尝试了你在问题中提到的,它的工作很好。看看你是否也这样做了。
html:
<div data-ng-app="myApp" data-ng-controller="myCtrl">
<div data-ng-repeat="pic in pictures">
{{pic.comments | json }} <button ng-click="addcomment($index)">add comment</button>
</div>
</div>
js代码:
(function() {
var app = angular.module('myApp',[]);
app.controller("myCtrl", function($scope,$window) {
$scope.pictures=[
{
name:"/images/profile2.jpg",
caption:"Food and Hunger",
votes:0,
User:"xyz@gmail.com",
comments:[],
theme:$scope.theme
},
{
name:"/images/profile3.jpg",
caption:"The wedding day",
votes:0,
User:"yamini@gmail.com",
comments:[],
theme:$scope.theme
},
{
name:"/images/profile4.jpg",
caption:"Mother's Care",
votes:0,
User:"fakeid@yahoo.com",
comments:[],
theme:$scope.theme
}];
$scope.addcomment=function (index) {
var com=$window.prompt('Please enter your comment');
$scope.pictures[index].comments.push(com);
}
});
})();
相关文章:
- 如何使一个Math.random数组元素比另一个数组元素更有可能被选中
- JavaScript数组到另一个页面上的PHP
- js - 如何根据对象数组中另一个键的值更改一个键的值
- 如何查找数组中另一个对象旁边的对象
- 将排序数组的数组元素移动到具有特殊逻辑的另一个位置
- 将数组从另一个javascript文件中获取到脚本中
- Javascript将数组元素交换到另一个索引并删除旧索引
- PHP/JS -如何移动数组到另一个页面不使用URL, FORM, SESSION, COOKIE参数
- 比较字符串数组和另一个字符串数组的优雅方法
- 如果一个数组有一个属性,则查找同一数组的另一个属性的值
- 创建jQuery数组变量和传输数组到另一个页面PHP
- 当另一个元素以相同字母开头时,从字符串数组中删除一个元素
- 如何用另一个给定的数组元素替换数组元素
- 我不能从php页面检索JSON数据数组到另一个
- 传递多维javascript数组到另一个页面
- If数组元素包含一个句号
- 写一个javascript数组到另一个域的json文件
- 函数返回数组到另一个函数>未捕获TypeError
- 如何使用json数组设计另一个类
- 减少对象数组:根据另一个字段的值限制相似字段