I am trying to create a directive that iterates through an array of vendor objects, each vendor has an comments array property. I wanted to add a text area in the directive that would populate the comments array in link method of the directive. I was not able to get the value from text area, what am I doing wrong
my controller >>
.controller("VendorController",function($scope){
$scope.vendors = [
{
id : 1,
name : "foo",
comments:[]
},
{
id : 2,
name : "boo",
comments:[]
}
]
})
my directive>>
.directive('vendorInterests',function(){
return{
restrict:'E',
replace: true,
scope: {
vendors: "=",
comment :"="
},
,
template:"<div>"+
"<div ng-repeat='vendor in vendors'><div>{{vendor.name}}"+
"</div>"+
"<div ng-repeat='comment in vendor.comments'>{{comment}}</div>"+
" <textarea rows='5' ng-model='comment'></textarea>"+
" <button ng-click='addComment(vendor)'>Add Comment</button>"+
"</div>",
link:function(scope, elem, attrs){
scope.addComment=function(vendor){
//scope.comment is coming empty, how do i pass the comment into
//this method and update the view
console.log(scope.comment);
//console.log(cntrl.$viewValue.comment);
vendor.comments.push(scope.comment);
}
}
}
});
my html>>
<body ng-app="dealApp">
<div ng-controller="VendorController">
<vendor-interests vendors="vendors"></vendor-interests>
</div>
</body>
Please find the plunker here