我是AngularJS的初学者,我试图使用下面的代码以及具有保存和取消按钮的模式形式来编辑表列表对象。
根据我的要求,当我单击“保存”按钮时,仅需要更新对象;如果我单击“取消”按钮,则该对象不应该更新,但是即使我不点击“保存”按钮,它也将自动更新。
我该怎么办?有人可以帮我吗。
<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>
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');
};
}]);
答案 0 :(得分:1)
您应该在控制器myController
内添加/编辑用户,为此,模式需要返回用户:
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');
};
}]);
我们使用angular.copy($scope.users);
的事实使我们可以确保模式不会更新您的用户列表。
此行:$uibModalInstance.close($scope.newUser)
将值返回给控制器。
然后,您的列表会在modalInstance.result.then
函数中进行更新。
答案 1 :(得分:-1)
我没有看到在HTML中的任何位置都添加了ng-app
或引用了控制器