ng-click single click select and remove item. in AngularJs
ng-click single click select and remove item. in AngularJs
by Ankit Patidar
HTML
<div ng-app="myApp">
<div ng-controller="MyCtrl">{{availableGroups}}
<div ng-repeat="g in quoteLibrary">{{g.name}} <a href='' ng-click='selectQuoteFun(g.id);'>+</a></div>
<hr>
<div ng-repeat="ag in quote.selectedQuote">{{ag.name}} <a href="" ng-click="selectedQuoteRemove($index);">X</a></div>
</div>
</div>
CSS
div {
white-space: pre;
}
JavaScript
var app = angular.module('myApp',[]);
app.controller('MyCtrl', function($scope) {
$scope.quoteLibrary = [{
id: 115,
name: 'Test Group 2',
Description: '',
owner: 10,
OwnerIsUser: false,
},{
id: 116,
name: 'Test Group 3',
Description: '',
owner: 71,
OwnerIsUser: false,
},{
id: 117,
name: 'Test Group 4',
Description: '',
owner: 71,
OwnerIsUser: false,
},{
id: 118,
name: 'Test Group 5',
Description: '',
owner: 115,
OwnerIsUser: false,
},{
id: 119,
name: 'Test Group 6',
Description: '',
owner: 8,
OwnerIsUser: true,
}];
$scope.quote=[];
var selectedQuoteIds = {};
var quoteLibraryIds = {};
var result;
$scope.quote.selectedQuote = [];
$scope.selectQuoteFun = function(id){
result = [];
angular.forEach($scope.quoteLibrary, function (el){
if(id == el.id){
$scope.quote.selectedQuote.push(el);
}
});
$scope.quote.selectedQuote = (function(){
angular.forEach($scope.quote.selectedQuote, function(el, index){
selectedQuoteIds[el.id] = $scope.quote.selectedQuote[index];
});
angular.forEach($scope.quoteLibrary, function(el, index){
quoteLibraryIds[el.id] = $scope.quoteLibrary[index];
});
for(var index in quoteLibraryIds){
if(selectedQuoteIds.hasOwnProperty(index)){
result.push(quoteLibraryIds[index]);
console.info(quoteLibraryIds[index]);
}
}
return result;
}());
}
$scope.selectedQuoteRemove = function(index){
console.error(selectedQuoteIds);
console.warn(quoteLibraryIds);
console.info($scope.quote.selectedQuote);
console.log(result);
selectedQuoteIds = {};
quoteLibraryIds = {};
result.splice(index,...