Javascript angularjs-如何在编辑项时禁用模型绑定
我有这样的观点:Javascript angularjs-如何在编辑项时禁用模型绑定,javascript,angularjs,Javascript,Angularjs,我有这样的观点: <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>
<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>
我不明白:当我在模态中编辑时,为什么在表格中,作为背景,我看到我的表格正在改变?为什么,没有绑定(我认为是这样)这是因为当您运行editContact函数时,您使$scope.person指向与userToCopy相同的对象,在本例中是$scope.user,因此当您更改$scope.person时,$scope.user也会更改 使用$scope.initialUser代替$scope.person,这样$scope.initialUser就是原始用户的副本,而不是同一个指针,因此此对象上的更改不会反映在另一个对象上。
<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) {
});
}
};
{{initialUser.FirstName}
$scope.updateContact=function(){
如果($scope.contactForm.$valid){
$http.put(settings.apiBaseUri+'/myapp/contacts',$scope.initialUser{
标题:{
“内容类型”:“应用程序/json”
}
})
.成功(功能(响应){
}).错误(功能(错误、状态){
});
}
};
请添加代码,我无法100%理解您的意思和更改位置,因此在关闭模式后,您需要将scope.user更新为新数据。这就是工作原理。“因此,在关闭模式后,您需要将scope.user更新为新数据。这就是工作原理”-哪种做法最适合这样做?您可以打开关闭按钮模式函数,该函数将使$scope.user等于$scope。initialUser或use events.this(我知道))只是我需要通过表进行迭代并找到该项,因为我没有存储scope.user
$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) {
});
}
};
<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) {
});
}
};