从html&使用angularjs在多个级别上建模
remove item from html & model on multiple levels with angularjs
我希望能够从作为模型的数组的任何级别中删除节点,但我无法做到这一点,除了数组中的第一个级别。
我举了一个例子。我对棱角分明是个新手,但我一直在寻找这个特定问题的解决方案,但似乎找不到。
HTML
<ul ng-app="myApp" ng-controller="Ctrl">
<li ng-repeat="item in myCards.majorBlocks">
<h2>Block: {{item.name}}, Order: {{item.order}} <a href="#" ng-click="whatFnc(whatArg)">Remove</a></h2>
<ul ng-if="item.name == 'brands'">
<li ng-repeat="brandBlock in item.brand_blocks">
<h3>Brand: {{brandBlock.name}} <a href="#" ng-click="whatFnc(whatArg)">Remove</a></h3>
<ul>
<li ng-repeat="product in brandBlock.products">
<h4>Product: {{product.title}} <a href="#" ng-click="whatFnc(whatArg)">Remove</a></h4>
</li>
</ul>
</li>
</ul>
</li>
</ul>
JS
angular.module('myApp', [])
.controller('Ctrl', function($scope) {
$scope.myCards = {
tagline: 'Tagline',
url: 'url',
title: 'Please help',
majorBlocks: [
{
"name": "cover",
"coverUrl": "http://www.url.com/cover/",
"coverImage": "http://www.url.com/cover/",
"order": '1'
},
{
"name": 'text',
"text": "Here's what's just arrived at LandmarkShops. Take a pick from these smart cameras by Samsung and Kodak to share your precious memories. Pre-order the Xbox One with 5 great games for just AED 199. Also, check out our selection of toys, travel systems and Back To School gear from Babyshop, and a range of smart tees from Splash!",
"order": '2'
},
{
'name': 'brands',
'order': '3',
'brand_blocks': [
{
'name': 'adidas',
'order': '1',
'products': [
{
url: 'url',
thumb: 'thumb',
title: 'Product title B',
price: 'price',
order: '1'
},
{
url: 'url',
thumb: 'thumb',
title: 'Product title A',
price: 'price',
order: '2'
},
{
url: 'url',
thumb: 'thumb',
title: 'title',
price: 'price',
order: '3'
}
]
},
{
'name': 'nike',
'order': '2',
'products': [
{
url: 'url',
thumb: 'thumb',
title: 'Product title 1',
price: 'price',
order: '1'
},
{
url: 'url',
thumb: 'thumb',
title: 'Product title 2',
price: 'price',
order: '2'
},
{
url: 'url',
thumb: 'thumb',
title: 'Product title 3',
price: 'price',
order: '3'
}
]
},
{
'name': 'quicksilver',
'order': '3',
'products': [
{
url: 'url',
thumb: 'thumb',
title: 'dddd',
title: 'Product title 6',
order: '1'
},
{
url: 'url',
thumb: 'thumb',
title: 'Product title 7',
price: 'price',
order: '2'
},
{
url: 'url',
thumb: 'thumb',
title: 'Product title 8',
price: 'price',
order: '3'
}
]
},
{
'name': 'vans',
'order': '4',
'products': [
{
url: 'url',
thumb: 'thumb',
title: 'Lorem',
price: 'price',
order: '1'
},
{
url: 'url',
thumb: 'thumb',
title: 'titleLorem dasdas',
price: 'price',
order: '2'
},
{
url: 'url',
thumb: 'thumb',
title: 'dasdsda dasda',
price: 'price',
order: '3'
}
]
}
]
}
]//hhe
};
});
小提琴http://jsfiddle.net/1pmucunL/
您需要定义whtFunc和whtargs如下:-
$scope.whatFnc=function(item,parent){
parent.splice(parent.indexOf(item), 1);
};
和HTML
<ul ng-app="myApp" ng-controller="Ctrl">
<li ng-repeat="item in myCards.majorBlocks">
<h2>Block: {{item.name}}, Order: {{item.order}} <a href="#" ng-click="whatFnc(item,myCards.majorBlocks)">Remove</a></h2>
<ul ng-if="item.name == 'brands'">
<li ng-repeat="brandBlock in item.brand_blocks">
<h3>Brand: {{brandBlock.name}} <a href="#" ng-click="whatFnc(brandBlock, item.brand_blocks)">Remove</a></h3>
<ul>
<li ng-repeat="product in brandBlock.products">
<h4>Product: {{product.title}} <a href="#" ng-click="whatFnc(product,brandBlock.products)">Remove</a></h4>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Js文件:http://jsfiddle.net/1pmucunL/1/
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- AngularJS:我可以跳过函数参数回调吗
- AngularJS UI路由器不能像ng路由器那样工作
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 如何包含特定于每个视图angularjs的javascript文件
- AngularJS:ng之后,重复$scope值未按预期更新
- AngularJS-在JSON选择器中使用变量名
- $window.ga在AngularJS事件中未定义
- AngularJS-使用'true'属性
- 打开一个模态并将其链接到AngularJS中的指令
- Angularjs代码未在匿名函数中运行
- angularjs+rails应用程序中未显示模板
- 我的AngularJS表达式没有'不起作用
- 来自Svc的数据未建模到视图元素AngularJS中
- 如何在JavaScript中对此数据结构进行建模?(AngularJS)
- 从html&使用angularjs在多个级别上建模
- Angularjs单元测试不从SELECT节点建模数据
- 使用promise在angularJS中建模异步数据
- AngularJS的单页应用程序-如何建模
- 如何使用AngularJS/Firebase建模数据-何时使用控制器?