I personally would like to decorate my $ scope with services (i.e. $ scope.modalService = ModalService;), so I understand the source of the logic. In ng-repeat, you then pass the value element to a method call:
<div class="input-group"> <input class="form-control" ng-model="value.value"> <span class="input-group-addon" ng-click="modalService.openTextEditModal(value)"> <span class="glyphicon glyphicon-align-justify"></span> </span> </div>
The modal service and the modal template will refer to the object, in this case, clone the object, which will help with state management, and not the text:
app.factory('ModalService', function($modal) { return { openTextEditModal: function(item) { var modalInstance = $modal.open({ templateUrl: 'modal.html', backdrop: 'static', controller: function($scope, $modalInstance, $sce, item) { var clone = {}; angular.copy(item, clone); $scope.clone = clone; $scope.close = function() { $modalInstance.dismiss('cancel'); }; $scope.save = function() { angular.extend(item, clone); $modalInstance.close(); }; }, size: 'lg', resolve: { item: function() { return item; } } }); } }; });
When changing the corresponding modal template:
<div class="modal-header"> <h3 class="modal-title">Edit text</h3> </div> <div class="modal-body"> <textarea class="form-control" ng-model="clone.value"></textarea> </div> <div class="modal-body"> <button class="btn btn-warning" ng-click="close()">Close</button> <button class="btn btn-success pull-right" ng-click="save()">Save</button> </div>
source share