<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.2.16/angular.js"></script>
<script src="script.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Title</title>
</head>
<body ng-app="app" ng-controller="OrderCtrl">
<div>
<span> Order: </span>
<select ng-model="current" ng-options="order.name for order in orders"></select>
</div><br/>
<div ng-repeat="part in orders | filter:current._id" ng-include="'parts.html'"></div>
<br/><br/><p class='gray'>{{orders}}</p>
</body>
</html>
angular.module('app', [])
.controller('OrderCtrl', function ($scope) {
function Order (name) {
this.name = name;
}
Order.prototype.addChild = function () {
this.parts = this.parts || [];
this.parts[this.parts.length] = new Order (this.name + '-' + (this.parts.length + 1));
};
Order.prototype.removeChild = function (index) {
if (index === undefined) {
delete this.parts;
} else if (index > -1 && index < this.parts.length) {
this.parts.splice(index, 1);
if (this.parts.length === 0) delete this.parts;
}
};
var prepareOrders = [];
prepareOrders[0] = new Order('part 1');
prepareOrders[0]['_id'] = '123456';
prepareOrders[1] = new Order('part 2');
prepareOrders[1]['_id'] = '234567';
$scope.orders = prepareOrders;
$scope.current = $scope.orders[0];
});
<span> Part: </span>
<input ng-model='part.name'/>
<span><a href='' ng-click='$parent.$parent.part.removeChild($index)'>Remove this</a></span>
<span><a href='' ng-click='part.addChild()'>Add child</a></span>
<span><a href='' ng-click='part.removeChild()'>Remove childs</a></span>
<div class='node' ng-repeat='part in part.parts' ng-include="'parts.html'"></div>
.node {
margin-left: 10px;
margin-top: 5px;
}
.gray {
color: #ccc;
}
span {
padding: 0 2px;
}
input {
border: none;
background: transparent;
border-bottom: 1px dotted #999;
}
a {
border: 1px solid #999;
display: inline-block;
padding: 2px 5px;
text-decoration: none;
color: #999;
}
a:hover {
box-shadow: 0 0 5px rgba(0,0,0,0.3);
background: linear-gradient(to bottom, #fcfff4, #e9e9ce);
color: #a00;
}