How to edit table list object in AngularJS
I am beginner in AngularJS and I am trying to edit table list object using below code and my modal form having save and cancel buttons.
As per my requirement when I click on save button then only object need to update and if I click on cancel button it should not update but using my below code even when I not tap on save button its automatically updating.
How can I do my requirement? Can some one help me please.
add_user.html
<div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">New User Registration</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Username" ng-model="newUser.username">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Email" ng-model="newUser.email">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Full Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Full Name" ng-model="newUser.fullName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="saveUser();">Save</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="close()">Close</button>
</div>
</div>
app.js
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return $scope.users;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: $scope.users,
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$scope.users = items;
$uibModalInstance.close();
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$scope.users[user.position] = $scope.newUser;
$uibModalInstance.close();
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angularjs
add a comment |
I am beginner in AngularJS and I am trying to edit table list object using below code and my modal form having save and cancel buttons.
As per my requirement when I click on save button then only object need to update and if I click on cancel button it should not update but using my below code even when I not tap on save button its automatically updating.
How can I do my requirement? Can some one help me please.
add_user.html
<div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">New User Registration</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Username" ng-model="newUser.username">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Email" ng-model="newUser.email">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Full Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Full Name" ng-model="newUser.fullName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="saveUser();">Save</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="close()">Close</button>
</div>
</div>
app.js
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return $scope.users;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: $scope.users,
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$scope.users = items;
$uibModalInstance.close();
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$scope.users[user.position] = $scope.newUser;
$uibModalInstance.close();
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angularjs
Can some one suggest me please
– Krish
Nov 16 '18 at 9:33
add a comment |
I am beginner in AngularJS and I am trying to edit table list object using below code and my modal form having save and cancel buttons.
As per my requirement when I click on save button then only object need to update and if I click on cancel button it should not update but using my below code even when I not tap on save button its automatically updating.
How can I do my requirement? Can some one help me please.
add_user.html
<div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">New User Registration</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Username" ng-model="newUser.username">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Email" ng-model="newUser.email">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Full Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Full Name" ng-model="newUser.fullName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="saveUser();">Save</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="close()">Close</button>
</div>
</div>
app.js
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return $scope.users;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: $scope.users,
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$scope.users = items;
$uibModalInstance.close();
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$scope.users[user.position] = $scope.newUser;
$uibModalInstance.close();
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angularjs
I am beginner in AngularJS and I am trying to edit table list object using below code and my modal form having save and cancel buttons.
As per my requirement when I click on save button then only object need to update and if I click on cancel button it should not update but using my below code even when I not tap on save button its automatically updating.
How can I do my requirement? Can some one help me please.
add_user.html
<div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">New User Registration</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Username" ng-model="newUser.username">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Email" ng-model="newUser.email">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Full Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Full Name" ng-model="newUser.fullName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="saveUser();">Save</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="close()">Close</button>
</div>
</div>
app.js
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return $scope.users;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: $scope.users,
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$scope.users = items;
$uibModalInstance.close();
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$scope.users[user.position] = $scope.newUser;
$uibModalInstance.close();
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angularjs
angularjs
edited Nov 16 '18 at 10:32
jess
924110
924110
asked Nov 16 '18 at 8:50
KrishKrish
1,16662056
1,16662056
Can some one suggest me please
– Krish
Nov 16 '18 at 9:33
add a comment |
Can some one suggest me please
– Krish
Nov 16 '18 at 9:33
Can some one suggest me please
– Krish
Nov 16 '18 at 9:33
Can some one suggest me please
– Krish
Nov 16 '18 at 9:33
add a comment |
2 Answers
2
active
oldest
votes
You should add/edit your user inside the controller myController
, to do so, the modal needs to return the user:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return angular.copy($scope.users);
}
}
});
modalInstance.result.then(function (newUser) {
$scope.users.push(newUser);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: angular.copy($scope.users),
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (user) {
$scope.users[index] = user
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
The fact that we use angular.copy($scope.users);
allows us to make sure the modal won't update your list of user.
This line: $uibModalInstance.close($scope.newUser)
returns the value to the controller.
Your list is then updated in the modalInstance.result.then
function.
Remaining my code is fine?
– Krish
Nov 16 '18 at 10:21
No, sorry, I updated my answer since you were saving your user inside the modal. It is bad practice, the modal should return the user and the controller should behave accordingly
– Groben
Nov 16 '18 at 10:23
very clear thanks a lot
– Krish
Nov 16 '18 at 10:39
why your returning arrayList: angular.copy($scope.users), instead of arrayList: $scope.users
– Krish
Nov 16 '18 at 10:59
please clarify my doubt
– Krish
Nov 16 '18 at 10:59
|
show 2 more comments
I dont see ng-app
being added or the controller being referenced anywhere in the HTML
controller being referenced
– Krish
Nov 16 '18 at 9:51
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
– Krish
Nov 16 '18 at 9:52
controller being referenced
Where?
– krishnanspace
Nov 16 '18 at 9:54
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
I'm not able to understand you.
– krishnanspace
Nov 16 '18 at 9:54
when i open modal form with out tap save button its updating table list object ,I want to update when i click save button
– Krish
Nov 16 '18 at 10:00
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334319%2fhow-to-edit-table-list-object-in-angularjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should add/edit your user inside the controller myController
, to do so, the modal needs to return the user:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return angular.copy($scope.users);
}
}
});
modalInstance.result.then(function (newUser) {
$scope.users.push(newUser);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: angular.copy($scope.users),
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (user) {
$scope.users[index] = user
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
The fact that we use angular.copy($scope.users);
allows us to make sure the modal won't update your list of user.
This line: $uibModalInstance.close($scope.newUser)
returns the value to the controller.
Your list is then updated in the modalInstance.result.then
function.
Remaining my code is fine?
– Krish
Nov 16 '18 at 10:21
No, sorry, I updated my answer since you were saving your user inside the modal. It is bad practice, the modal should return the user and the controller should behave accordingly
– Groben
Nov 16 '18 at 10:23
very clear thanks a lot
– Krish
Nov 16 '18 at 10:39
why your returning arrayList: angular.copy($scope.users), instead of arrayList: $scope.users
– Krish
Nov 16 '18 at 10:59
please clarify my doubt
– Krish
Nov 16 '18 at 10:59
|
show 2 more comments
You should add/edit your user inside the controller myController
, to do so, the modal needs to return the user:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return angular.copy($scope.users);
}
}
});
modalInstance.result.then(function (newUser) {
$scope.users.push(newUser);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: angular.copy($scope.users),
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (user) {
$scope.users[index] = user
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
The fact that we use angular.copy($scope.users);
allows us to make sure the modal won't update your list of user.
This line: $uibModalInstance.close($scope.newUser)
returns the value to the controller.
Your list is then updated in the modalInstance.result.then
function.
Remaining my code is fine?
– Krish
Nov 16 '18 at 10:21
No, sorry, I updated my answer since you were saving your user inside the modal. It is bad practice, the modal should return the user and the controller should behave accordingly
– Groben
Nov 16 '18 at 10:23
very clear thanks a lot
– Krish
Nov 16 '18 at 10:39
why your returning arrayList: angular.copy($scope.users), instead of arrayList: $scope.users
– Krish
Nov 16 '18 at 10:59
please clarify my doubt
– Krish
Nov 16 '18 at 10:59
|
show 2 more comments
You should add/edit your user inside the controller myController
, to do so, the modal needs to return the user:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return angular.copy($scope.users);
}
}
});
modalInstance.result.then(function (newUser) {
$scope.users.push(newUser);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: angular.copy($scope.users),
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (user) {
$scope.users[index] = user
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
The fact that we use angular.copy($scope.users);
allows us to make sure the modal won't update your list of user.
This line: $uibModalInstance.close($scope.newUser)
returns the value to the controller.
Your list is then updated in the modalInstance.result.then
function.
You should add/edit your user inside the controller myController
, to do so, the modal needs to return the user:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath@gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim@gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim@gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return angular.copy($scope.users);
}
}
});
modalInstance.result.then(function (newUser) {
$scope.users.push(newUser);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: angular.copy($scope.users),
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (user) {
$scope.users[index] = user
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
The fact that we use angular.copy($scope.users);
allows us to make sure the modal won't update your list of user.
This line: $uibModalInstance.close($scope.newUser)
returns the value to the controller.
Your list is then updated in the modalInstance.result.then
function.
edited Nov 16 '18 at 10:22
answered Nov 16 '18 at 10:09
GrobenGroben
1,0061823
1,0061823
Remaining my code is fine?
– Krish
Nov 16 '18 at 10:21
No, sorry, I updated my answer since you were saving your user inside the modal. It is bad practice, the modal should return the user and the controller should behave accordingly
– Groben
Nov 16 '18 at 10:23
very clear thanks a lot
– Krish
Nov 16 '18 at 10:39
why your returning arrayList: angular.copy($scope.users), instead of arrayList: $scope.users
– Krish
Nov 16 '18 at 10:59
please clarify my doubt
– Krish
Nov 16 '18 at 10:59
|
show 2 more comments
Remaining my code is fine?
– Krish
Nov 16 '18 at 10:21
No, sorry, I updated my answer since you were saving your user inside the modal. It is bad practice, the modal should return the user and the controller should behave accordingly
– Groben
Nov 16 '18 at 10:23
very clear thanks a lot
– Krish
Nov 16 '18 at 10:39
why your returning arrayList: angular.copy($scope.users), instead of arrayList: $scope.users
– Krish
Nov 16 '18 at 10:59
please clarify my doubt
– Krish
Nov 16 '18 at 10:59
Remaining my code is fine?
– Krish
Nov 16 '18 at 10:21
Remaining my code is fine?
– Krish
Nov 16 '18 at 10:21
No, sorry, I updated my answer since you were saving your user inside the modal. It is bad practice, the modal should return the user and the controller should behave accordingly
– Groben
Nov 16 '18 at 10:23
No, sorry, I updated my answer since you were saving your user inside the modal. It is bad practice, the modal should return the user and the controller should behave accordingly
– Groben
Nov 16 '18 at 10:23
very clear thanks a lot
– Krish
Nov 16 '18 at 10:39
very clear thanks a lot
– Krish
Nov 16 '18 at 10:39
why your returning arrayList: angular.copy($scope.users), instead of arrayList: $scope.users
– Krish
Nov 16 '18 at 10:59
why your returning arrayList: angular.copy($scope.users), instead of arrayList: $scope.users
– Krish
Nov 16 '18 at 10:59
please clarify my doubt
– Krish
Nov 16 '18 at 10:59
please clarify my doubt
– Krish
Nov 16 '18 at 10:59
|
show 2 more comments
I dont see ng-app
being added or the controller being referenced anywhere in the HTML
controller being referenced
– Krish
Nov 16 '18 at 9:51
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
– Krish
Nov 16 '18 at 9:52
controller being referenced
Where?
– krishnanspace
Nov 16 '18 at 9:54
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
I'm not able to understand you.
– krishnanspace
Nov 16 '18 at 9:54
when i open modal form with out tap save button its updating table list object ,I want to update when i click save button
– Krish
Nov 16 '18 at 10:00
|
show 3 more comments
I dont see ng-app
being added or the controller being referenced anywhere in the HTML
controller being referenced
– Krish
Nov 16 '18 at 9:51
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
– Krish
Nov 16 '18 at 9:52
controller being referenced
Where?
– krishnanspace
Nov 16 '18 at 9:54
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
I'm not able to understand you.
– krishnanspace
Nov 16 '18 at 9:54
when i open modal form with out tap save button its updating table list object ,I want to update when i click save button
– Krish
Nov 16 '18 at 10:00
|
show 3 more comments
I dont see ng-app
being added or the controller being referenced anywhere in the HTML
I dont see ng-app
being added or the controller being referenced anywhere in the HTML
answered Nov 16 '18 at 9:48
krishnanspacekrishnanspace
368
368
controller being referenced
– Krish
Nov 16 '18 at 9:51
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
– Krish
Nov 16 '18 at 9:52
controller being referenced
Where?
– krishnanspace
Nov 16 '18 at 9:54
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
I'm not able to understand you.
– krishnanspace
Nov 16 '18 at 9:54
when i open modal form with out tap save button its updating table list object ,I want to update when i click save button
– Krish
Nov 16 '18 at 10:00
|
show 3 more comments
controller being referenced
– Krish
Nov 16 '18 at 9:51
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
– Krish
Nov 16 '18 at 9:52
controller being referenced
Where?
– krishnanspace
Nov 16 '18 at 9:54
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
I'm not able to understand you.
– krishnanspace
Nov 16 '18 at 9:54
when i open modal form with out tap save button its updating table list object ,I want to update when i click save button
– Krish
Nov 16 '18 at 10:00
controller being referenced
– Krish
Nov 16 '18 at 9:51
controller being referenced
– Krish
Nov 16 '18 at 9:51
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
– Krish
Nov 16 '18 at 9:52
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
– Krish
Nov 16 '18 at 9:52
controller being referenced
Where?– krishnanspace
Nov 16 '18 at 9:54
controller being referenced
Where?– krishnanspace
Nov 16 '18 at 9:54
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
I'm not able to understand you.– krishnanspace
Nov 16 '18 at 9:54
my requirement when i click on save button then only object need to update and if i click on cancel button it should not update but using my below code even when i not tap on save button its automatically updating
I'm not able to understand you.– krishnanspace
Nov 16 '18 at 9:54
when i open modal form with out tap save button its updating table list object ,I want to update when i click save button
– Krish
Nov 16 '18 at 10:00
when i open modal form with out tap save button its updating table list object ,I want to update when i click save button
– Krish
Nov 16 '18 at 10:00
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334319%2fhow-to-edit-table-list-object-in-angularjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can some one suggest me please
– Krish
Nov 16 '18 at 9:33