如何在编辑项目时禁用模型绑定
how to disable model binding when editing item
我有这样一部分视图:
<tbody>
<tr data-ng-repeat="user in users | orderBy:predicate:reverse" data-id="{{user.Id}}" class="table-row">
<td>
<div class="contacts-cell">
{{user.FirstName}}
</div>
</td>
<td>
<a href="javascript:void(0)" data-ng-click="editContact(user)" data-toggle="modal" data-target="#new-person"></a>
</td>
</tr>
</tbody>
以及我编辑数据的模式:
<div class="modal custom" id="new-person" tabindex="-1" role="dialog" aria-hidden="true" modal-center data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal first-modal" role="form" name="contactForm" novalidate>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" data-ng-click="onCancel()"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
</div>
<div class="modal-body">
<div class="column-left">
<div class="form-group" data-ng-class="{ 'has-error': submittedContact && contactForm.firstName.$invalid }">
<label class="control-label" for="name">Name</label>
<span class="input-nonedit" data-ng-show="isShowWindow">{{person.FirstName}}</span>
<input type="text" id="name" class="form-control on-input" data-ng-model="person.FirstName" name="firstName" placeholder="Enter Name" data-ng-hide="isShowWindow" maxlength="100" required/>
</div>
</div>
</div>
<div class="column-right">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn-edit pull-right" data-ng-click="updateContact()" data-target="#new-person">Edit</button>
</div>
</form>
</div>
</div>
</div>
和控制器:
$scope.editContact = function(userToCopy) {
$scope.person = userToCopy;
$scope.initialUser = angular.copy(userToCopy);
};
$scope.updateContact = function() {
if ($scope.contactForm.$valid) {
$http.put(settings.apiBaseUri + '/myapp/contacts', $scope.person, {
headers: {
'Content-Type': 'application/json'
}
})
.success(function(response) {
}).error(function(err, status) {
});
}
};
我不明白:当我在模态中编辑时,为什么在表中,作为背景,我看到我的表正在改变?为什么,没有绑定(我认为是这样)
这是因为当您运行editContact函数时,您会使$scope.person指向与userToCopy相同的对象,在本例中为$scope.user,因此当您更改$scope.person时,$scope.user也会更改。
使用$scope.initialUser而不是$scope.person,这样$scope.internalUser是原始用户的副本,而不是同一个指针,因此对此对象的更改不会反映在另一个对象上
<span class="input-nonedit" data-ng-show="isShowWindow">{{initialUser.FirstName}}</span>
$scope.updateContact = function() {
if ($scope.contactForm.$valid) {
$http.put(settings.apiBaseUri + '/myapp/contacts', $scope.initialUser, {
headers: {
'Content-Type': 'application/json'
}
})
.success(function(response) {
}).error(function(err, status) {
});
}
};
相关文章:
- Backbone.js将模型绑定到视图时出错
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- 当显式定义控制器参数时,默认模型绑定器会发生异常
- NG-重复中断指令模型绑定
- 模型绑定重置后,Angularjs 电子邮件表单字段未清除/重置
- 如何将角度动画延迟到模型绑定之后
- 模型如何将Javascript FormData与Asp.net Controllers模型绑定
- Angular Js将文本输入与模型绑定
- ASP.Net MVC模型绑定到javascript
- 如何在编辑项目时禁用模型绑定
- 如何在AngularJS中创建类似字典的模型绑定
- ng模型绑定无法使用ng模板
- 模型绑定在 AngularJS 中不同步
- 如何将指令模型绑定到异步服务数据
- 挖空视图模型绑定未定义错误
- JS框架如何将模型绑定到视图
- AngularJS将模型绑定到ngRepeat(并对其进行评估)
- 将模型绑定到模板的控件
- AngularJS和Play框架模型绑定和模板
- MVC 模型绑定无法通过 AJAX 请求工作