Reputation: 1765
I am using angular directives to create a little complex form.
<div class="row">
<div class="col-xs-4">
<span></span>
</div>
<div class="col-xs-8" ng-controller="newOrdercontroller" >
<form class="form-horizontal" ng-submit="checkvalues(order)">
<div id="items-container">
<new-generic-item item-type="ecommerce" deletable="false" item-id="1"></new-generic-item>
</div>
<button class="btn btn-primary" ng-click="addElement($event)">Add Item</button>
<button class="btn" type="submit" ></button>
</form>
</div>
</div>
evidently, new-generic-item
is the directive i'm using. This directive creates multiple form elements.
.directive("newGenericItem", function() {
return {
restrict: 'E',
templateUrl: 'static/js/directives/genericItem.html',
controller: function($scope, $element, $attrs){
var itemType = $attrs['itemType'];
var itemId = $attrs['itemId'];
var deletable = $attrs['deletabe'];
$scope.items = $scope.itemTypes[itemType];
},
scope:true
}
})
this produces the following form.
so with Add Item
button I managed to add the multiple directives.
this is the reason why i used scope:true
in my directive factory.
The problem comes with this using of different scopes. I am having a hard time submitting the form. What would be the easiest way to access all the values of this form.
I have come simple solutions where in directive is used to create single simple form-element. (http://jsfiddle.net/revolunet/9e7Hy/)
My situation is different as it involves multiple form elements.
incase, having the controller code is useful
.controller('newOrdercontroller', function($scope, $compile, itemTypes){
$scope.itemTypes = itemTypes;
$scope.addElement = function(event){
var element = angular.element(document.querySelector("#items-container"));
element.append($compile('<new-generic-item item-type="ecommerce"></new-generic-item>')($scope));
}
$scope.checkvalues = function(order){
console.log($scope);
}
});
Upvotes: 0
Views: 75
Reputation: 9476
<new-generic-item ng-repeat="item in items" item="item"></new-generic-item> <button ng-click="addNewItem()">Add</button>
in controller: $scope.items = [{type: ..., id: ...}]; $scope.addNewItem = function() { $scope.items.push({new object}); } In directive:
scope: { item : '='}
Add just use 'item' in template, like
<input ng-model="item.type">
Then in submit you have all your data in array and can easily do whatever you want.
Upvotes: 0
Reputation: 902
Using events :
Example... Child scope:
$scope.$emit('runApplication', parameters);
On one of your ancestors scope (definitely sure that is not your parent ) Actually I don't know what's in par2, maybe the scope of the directive firing the event. It's still some test code :)
$scope.$on('runApplication', function(par2, parameters) {
});
Upvotes: 1