Javascript ng显示不在嵌套ng重复上工作
我有这样的JSON结构:Javascript ng显示不在嵌套ng重复上工作,javascript,html,angularjs,Javascript,Html,Angularjs,我有这样的JSON结构: { "products": { "318": { "id": 318, "product_id": 32, "sold_by": 1, "sold_from_date": 1452075077, "sold_to_date": 1459937477, "product_price": "1,200", "renewed_for": 0, "renewed": {
{
"products": {
"318": {
"id": 318,
"product_id": 32,
"sold_by": 1,
"sold_from_date": 1452075077,
"sold_to_date": 1459937477,
"product_price": "1,200",
"renewed_for": 0,
"renewed": {
"319": {
"id": 319,
"product_id": 32,
"sold_by": 1,
"sold_from_date": 1452075077,
"sold_to_date": 1459937477,
"product_price": "1,200",
"renewed_for": 318
}
}
}
}
}
function MyCtrl($scope) {
$scope.dataUI = {
showEdit: true
};
}
<div ng-repeat=data in products>
<div class=edit ng-click="dataUI.showEdit = true">
</div>
<div ng-repeat=renew in data>
<div class=edit ng-click="dataUI.showEdit = true">
</div>
</div>
<div class="modal" ng-show="dataUI.showEdit">
//Code for edit modal
</div>
</div>
要打印数据,我必须循环两次
第一个是打印318细节的外部循环。下一个是319,它是318的父母。父项是通过索引的更新_决定的
在ng repeat的第一块中,我有一些选项,如编辑和删除。单击这些按钮,将打开一个弹出窗口。
在ng repeat的第二个(内部)块中,编辑和删除选项相同
<div ng-repeat=data in products>
<div class=edit ng-click="showEdit = true">
</div>
<div ng-repeat=renew in data>
<div class=edit ng-click="showEdit = true">
</div>
</div>
<div class="modal" ng-show="showEdit">
<div class="product_price">{{data.product_price}}</div>
</div>
</div>
{{data.product{u price}}
showEdit弹出窗口仅适用于外部循环,而不适用于内部(续订)循环
编辑:现在,当我从内部循环(更新)打开弹出窗口时,我在其中得到的值就是外部循环(数据)的值。如何解决这个问题???ng repeat创建了一个新范围。 你必须这样做
$parent.showEdit = true
在内侧,重复ng
但更好的方法是首先不要使用“未设置的”变量。看这里:
下面是一个工作示例:
<div ng-repeat=data in products>
<div class=edit ng-click="showEdit = true; content = data;">
</div>
<div ng-repeat=renew in data>
<div class=edit ng-click="$parent.showEdit = true; $parent.content = renew;">
</div>
</div>
<div class="modal" ng-show="$parent.showEdit">
<div class="product_price">{{content.product_price}}</div>
</div>
</div>
{{content.product_price}
ng repeat创建一个新范围。
你必须这样做
$parent.showEdit = true
在内侧,重复ng
但更好的方法是首先不要使用“未设置的”变量。看这里:
下面是一个工作示例:
<div ng-repeat=data in products>
<div class=edit ng-click="showEdit = true; content = data;">
</div>
<div ng-repeat=renew in data>
<div class=edit ng-click="$parent.showEdit = true; $parent.content = renew;">
</div>
</div>
<div class="modal" ng-show="$parent.showEdit">
<div class="product_price">{{content.product_price}}</div>
</div>
</div>
{{content.product_price}
您必须在控制器内的$scope
上设置一个包含布尔值的对象,而不是布尔值,如下所示:
{
"products": {
"318": {
"id": 318,
"product_id": 32,
"sold_by": 1,
"sold_from_date": 1452075077,
"sold_to_date": 1459937477,
"product_price": "1,200",
"renewed_for": 0,
"renewed": {
"319": {
"id": 319,
"product_id": 32,
"sold_by": 1,
"sold_from_date": 1452075077,
"sold_to_date": 1459937477,
"product_price": "1,200",
"renewed_for": 318
}
}
}
}
}
function MyCtrl($scope) {
$scope.dataUI = {
showEdit: true
};
}
<div ng-repeat=data in products>
<div class=edit ng-click="dataUI.showEdit = true">
</div>
<div ng-repeat=renew in data>
<div class=edit ng-click="dataUI.showEdit = true">
</div>
</div>
<div class="modal" ng-show="dataUI.showEdit">
//Code for edit modal
</div>
</div>
函数MyCtrl($scope){
$scope.dataUI={
showEdit:true
};
}
//编辑模式代码
许多帖子解释了原因:
您必须在控制器内的
$scope
上设置一个包含布尔值的对象,而不是布尔值,如下所示:
{
"products": {
"318": {
"id": 318,
"product_id": 32,
"sold_by": 1,
"sold_from_date": 1452075077,
"sold_to_date": 1459937477,
"product_price": "1,200",
"renewed_for": 0,
"renewed": {
"319": {
"id": 319,
"product_id": 32,
"sold_by": 1,
"sold_from_date": 1452075077,
"sold_to_date": 1459937477,
"product_price": "1,200",
"renewed_for": 318
}
}
}
}
}
function MyCtrl($scope) {
$scope.dataUI = {
showEdit: true
};
}
<div ng-repeat=data in products>
<div class=edit ng-click="dataUI.showEdit = true">
</div>
<div ng-repeat=renew in data>
<div class=edit ng-click="dataUI.showEdit = true">
</div>
</div>
<div class="modal" ng-show="dataUI.showEdit">
//Code for edit modal
</div>
</div>
函数MyCtrl($scope){
$scope.dataUI={
showEdit:true
};
}
//编辑模式代码
许多帖子解释了原因:
张贴您的控制器以及如何打开弹出窗口。或者创建一个JSFIDLE。内部循环是否正在运行。。。我的意思是你能看到内环的div吗?张贴你的控制器和你如何打开弹出窗口。或者创建一个JSFIDLE。内部循环是否正在运行。。。我的意思是你能看到内部循环的div吗?最好在模型中使用一个对象而不是原语,并且避免使用
$parent
。嵌套的ng repeat
将需要$parent.$parent
,这会因为结构变化而变得很难看。这就是为什么我说不应该使用“undotted”变量并链接到另一个主题:)我已经回答了你的问题。还有你编辑的问题。这有着完全相同的原因。ng repeat创建了一个新的作用域,这就是为什么代码的行为与它的行为相似。您应该阅读我发布的链接,并相应地修改代码(使用虚线变量,如“myobject.showEdit”,而不是“showEdit”),但在我的模式中,我使用数据
来显示项目。在我的第二个循环中,它被更新的替换。那么,它将如何考虑更新的
对象?ng repeat的问题不是吗?最好在模型中使用对象而不是原语,并避免使用$parent
。嵌套的ng repeat
将需要$parent.$parent
,这会因为结构变化而变得很难看。这就是为什么我说不应该使用“undotted”变量并链接到另一个主题:)我已经回答了你的问题。还有你编辑的问题。这有着完全相同的原因。ng repeat创建了一个新的作用域,这就是为什么代码的行为与它的行为相似。您应该阅读我发布的链接,并相应地修改代码(使用虚线变量,如“myobject.showEdit”,而不是“showEdit”),但在我的模式中,我使用数据
来显示项目。在我的第二个循环中,它被更新的替换。那么,它将如何考虑更新的
对象呢?ng repeat的问题不是吗?