我通过 ng-repeat 制作了任务表,表中的每个任务都可以修改。任务表必须使用更新的任务进行更新。因此,我们需要访问特定的ng-repeat元素。我想知道如何访问特定的ng-repeat元素并使用新任务 ng-click = editTask()更新它。
请参阅$scope.editTask
,我想在$http.put(uri, data)
内进行更新。
工作流:
ng-click = beginTask(任务)打开对话框,在对话框中有ng-click = editTask(),它将通过$ http.put修改任务...
请参阅DEMO
<tr ng-repeat="task in tasks">
<td>{{task.title}}</td>
<td>{{task.description}}</td>
<td>
<a class="btn" data-toggle="modal" ng-click="beginEdit(task)">Edit</a>
</td>
</tr>
Angularjs代码
$scope.beginEdit=function(task){
$scope.title = task.title;
$scope.description=task.description;
$scope.done=task.done;
$scope.uri=task.uri;
$scope.index=$scope.tasks.indexOf(task);
$('#edit').modal('show');
};
$scope.editTask = function() {
title=$scope.title;
description=$scope.description;
done=$scope.done;
uri=$scope.uri;
$('#edit').modal('hide');
var i=$scope.index;
var data={title: title, description: description, done: done };
$http.put(uri, data)
.success(function(){
alert("Success");
});
};
答案 0 :(得分:3)
请检查一下 - :http://plnkr.co/edit/lVkWEsAGVLTY7mGfHP5N?p=preview
添加
$scope.tasks[$scope.index] = data;
在editTask中
$scope.editTask = function(obj) {
alert($scope.title);
title = $scope.title;
description = $scope.description;
done = $scope.done;
uri = $scope.uri;
$('#edit').modal('hide');
var i = $scope.index;
var data = {
title: title,
description: description,
done: done
};
alert("uri" + uri);
alert(data.title);
$scope.tasks[$scope.index] = data; // For updating value
$http.put(uri, data)
.success(function() {
//tasks[i].uri(data.uri);
alert("Success");
});
};