I created a plunker http://plnkr.co/edit/1kvYuxD3PpUXvB0G5W33?p=preview
i want to create a directive which can take a scope field and with add button i want to push some value in the scope field. here are the two problems
I am not able to attach event on button but able to attach event on Main directive element. I know i can use jquery's delegation but that's not the right way.
(Solved) When event is attached i am updating scope but the problem is its not updating layout untill something changed in $scope or submitted the form . (Shivkumar's comment solved the problem )
I want to make this directive reusable i.e
i want to have multiple use of this directive for different fields. My directive name is arrayfield
<div arrayfield model="employee.docs" title="Documents"></div>
<div arrayfield model="employee.docsPrimary" title="Primary Documents"></div>
Code for directive.
app.directive('arrayfield', function() {
return {
restrict: 'EA',
scope: {
data: '=model',
title: '@'
},
link:function link(scope, element, attrs) {
//always gettign this emply
console.log(element.find("#addBtn"));
scope.$watch('data',function(){
console.log("SOmething updated");
});
element.bind("click",function(){
//If data exist push are assign new one
(scope.data && scope.data.push({})) || (scope.data=[{}]);
scope.$apply(); // thanks Shivkumar for answring in comment
});
},
templateUrl: 'arrayField.html'
};
});
Please let me know if you need more information.
to replicate issue i you can click on add document and then type anything in the other fields new element should be added but i want to add it as soon as i click on add document button