In order for ngClick to take effect, you need to first compile your HTML and link it to the $scope:
.controller('someCtrl', function ($compile, $scope) {
...
$scope.addProduct = function($event, obj) {
var myEl = angular.element($event.target);
var myBtn = $compile('<button class="btn btn-danger btn-xs" ng-click="removeProduct('+ obj.id +')">Remove from Widget</button>')($scope);
myEl.after(myBtn);
};
$scope.removeProduct = function(id) {
console.log('Remove ->' + id);
};
});
UPDATE:
Kos Prov's comment is very true - sometimes you think about solving the problem, but you don't think the question was wrong in the first place.
So, of course manipulating the DOM should be a directive's responsibility, so I created a directive to do what you want (according to your question):
app.directive('removableProduct', function ($compile) {
var btnTmpl =
'' +
'Remove from Widget' +
'';
return {
restrict: 'A',
scope: {item: '='},
template: '<div ng-click="addProduct($event)">{{item.description}}</div>',
controller: function ($scope) {
$scope.addProduct = function (evt) {
var myEl = angular.element(evt.target);
var myBtn = $compile(btnTmpl)($scope);
myEl.after(myBtn);
};
$scope.removeProduct = function(id) {
console.log('Remove -> ' + id);
};
}
};
});
See, also, this short demo.
ng-showorng-if). Have a boolean variable like$scope.viewState.productAddedto control which button is visible.