To begin with, if you intend to reuse the <foo>
element, it's advisable to establish an isolated scope:
.directive("foo", function(){
return {
restrict: "E",
scope: {
data: "="
},
template: "<div><label>Enter foo: </label><input ng-model='data'/></div>"
}
});
Creating a custom directive is similar to defining other tags. Given the limited context provided, here is a suggestion:
app.controller("MainCtrl", function($scope)){
$scope.fooModels = [];
$scope.addFoo = function(){
$scope.fooModels.push(new FooModel());
};
}
The FooModel()
function serves as a placeholder for the desired foo data model. Alternatively, you can simply use $scope.fooModels.push({});
.
Subsequently, in the view, use ng-repeat
to iterate through your fooModels:
<div ng-repeat="fooModel in fooModels">
<foo data="fooModel.data"></foo>
</div>
<button ng-click="addFoo()">Add Foo</button>
Feel free to explore and experiment with this concept in a plunker.